开发者

How is unsigned int/long represented

开发者 https://www.devze.com 2022-12-22 10:10 出处:网络
I was reading some assembly tutorial in which there were explained the signed integers and the unsigned integers and the difference between their representation in computer memory.

I was reading some assembly tutorial in which there were explained the signed integers and the unsigned integers and the difference between their representation in computer memory.

I remember something like that there was some bit at the begi开发者_高级运维nning at the number so it tells whether the integer is unsigned or signed.

If someone knows it, please answer.


Wikipedia's page on Signed Number representation shows the most common memory implementations of both signed and unsigned integer values.

Two's complement is the most common representation.


There are many possible representations of signed numbers, the most common is two's complement.


The MSB does not determine whether or not the number is signed; in signed numbers it represents whether the number is negative. In unsigned numbers it's just the MSB. It's the program that determines whether a number is considered signed or unsigned.


he he. tricky question. signed and unsigned integers are represented exactly same. looking at memory you will never be able to distinguish them. it comes out of one of Von Neumann's principle. So where is difference? Difference is in the way they are interpreted. If variable is signed compiler uses commands for signed ints. Also if we know that number is signed it's first bit tells us it's sign. the conversion is done as ~i + 1 (c syntax) in both ways


There are many possible representations, each one depending on your computer's organization. The most famous are

  • One's complement
  • Two's complement

On both representations, you can test the first (most significant) bit to find out whether the number is positive (usually off) or negative (bit on). That is, if you treat the number as signed. If you tell the compiler to treat such numbers as unsigned, the sign bit is used as data (which doubles the maximum range for the data type).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号