C中的简单字符解释 [英] Simple Character Interpretation In C

查看:27
本文介绍了C中的简单字符解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

 #include<stdio.h>

 void main()
 {
     char ch = 129;
     printf("%d", ch);
 }

我得到的输出为 -127.什么意思?

I get the output as -127. What does it mean?

推荐答案

说明 char 是一个 8 位变量,只能容纳 2^8 = 256 个值,因为声明是 char chch是一个signed变量,也就是说它可以存储127个负值和正值.当您要求超过 127 时,该值从 -128 开始.

It means that char is an 8-bit variable that can only hold 2^8 = 256 values, since the declaration is char ch, ch is a signed variable, which means it can store 127 negative and positive values. when you ask to go over 127 then the value starts over from -128.

把它想象成一些街机游戏,你从屏幕的一侧移动到另一侧:

Think of it like some arcade games where you go from one side of the screen to the other:

ch = 50;

                                    ----->                        50 is stored
      |___________________________________|___________|           since it fits
    -128                       0         50          127          between -127
                                                                  and 128

<小时>

ch = 129;

                                                    ---           129 goes over
      -->                                                         127 by 2, so
      |__|____________________________________________|           it 'lands' in
    -128  -127                 0                     127          -127

但是!!你不应该依赖它,因为它是未定义的行为!

<小时>

为了纪念 Luchian Grigore,以下是正在发生的事情的位表示:

BUT!! you shouldn't rely on this since it's undefined behaviour!


In honor of Luchian Grigore here's the bit representation of what's happening:

char 是一个保存 8 位或一个字节的变量.因此,我们有 8 个 0 和 1 努力代表您想要的任何值.如果 charsigned 变量,它将表示它是正数还是负数.您可能读过有关表示符号的一位,这是对真实过程的抽象;事实上,它只是在电子领域实施的首批解决方案之一.但是这样一个微不足道的方法有一个问题,你会有2种方式来表示0(+0和-0):

A char is a variable that will hold 8-bits or a byte. So we have 8 0's and 1's struggling to represent whatever value you desire. If the char is a signed variable it will represent whether it's a positive or negative number. You probably read about the one bit representing the sign, that's an abstraction of the true process; in fact it is only one of the first solutions implemented in electronics. But such a trivial method had a problem, you would have 2 ways of representing 0 (+0 and -0):

0 0000000     ->    +0        1 0000000     ->    -0                    
^                             ^ 
|_ sign bit 0: positive       |_ sign bit 1: negative

保证不一致!!因此,一些非常聪明的人想出了一个称为 Ones' Complement 的系统,它将一个负数表示为其正数的否定(非运算):

Inconsistencies guaranteed!! So, some very smart folks came up with a system called Ones' Complement which would represent a negative number as the negation (NOT operation) of its positive counterpart:

01010101      ->    +85
10101010      ->    -85

这个系统...有同样的问题.0 可以表示为 00000000 (+0) 和 11111111 (-0).然后出现了一些更聪明的人,他们创建了二的补码,它将保留早期方法的否定部分,然后加 1,因此删除了那个讨厌的 -0 并为我们的范围提供了一个闪亮的新数字:-128!.那么我们的范围现在看起来如何?

This system... had the same problem. 0 could be represented as 00000000 (+0) and 11111111 (-0). Then came some smarter folks who created Two's Complement, which would hold the negation part of the earlier method and then add 1, therefore removing that pesky -0 and giving us a shiny new number to our range: -128!. So how does our range look now?

00000000     +0
00000001     +1
00000010     +2
...
01111110     +126
01111111     +127
10000000     -128
10000001     -127
10000010     -126
...
11111110     -2
11111111     -1

因此,当我们的小处理器尝试将数字添加到我们的变量时,这应该可以让您了解发生了什么:

So, this should give an idea of what's happening when our little processor tries to add numbers to our variable:

 0110010     50                   01111111     127
+0000010    + 2                  +00000010    +  2
 -------     --                   --------     ---
 0110100     52                   10000001    -127
     ^                                  ^       ^
     |_ 1 + 1 = 10          129 in bin _|       |_ wait, what?!

是的,如果您查看上面的范围表,您可以看到最多 127 (01111111) 的二进制文件很好而且很漂亮,没有什么奇怪的事情发生,但是在第 8 位设置为-128 (10000000) 解释的数字不再保持二进制大小,而是二进制补码表示.这意味着,二进制表示,变量中的位,1 和 0,我们心爱的 char 的核心,确实包含 129……它在那里,看看它!但是邪恶的处理器读取到微不足道的 -127 导致变量 HAD 被 signed 破坏了它在欧几里得一维空间中通过实数线的臭味移位的所有积极潜力.

Yep, if you review the range table above you can see that up to 127 (01111111) the binary was fine and dandy, nothing weird happening, but after the 8'th bit is set at -128 (10000000) the number interpreted no longer held to its binary magnitude but to the Two's Complement representation. This means, the binary representation, the bits in your variable, the 1's and 0's, the heart of our beloved char, does hold a 129... its there, look at it! But the evil processor reads that as measly -127 cause the variable HAD to be signed undermining all its positive potential for a smelly shift through the real number line in the Euclidean space of dimension one.

这篇关于C中的简单字符解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆