Signed number and unsigned number in C language

Source: Internet
Author: User

We know that there are unsigned and signed numbers in the C language (some high-level languages like Java have no unsigned numbers), but for computers, they do not differentiate between signed numbers and unsigned numbers, because they are 0 or 1 in the computer. However, we sometimes need to use a signed number to represent an integer in our actual use, so we specify that when the highest bit is 1, it is negative, and the highest bit is 0 o'clock, which is expressed as positive.

1: The difference between a signed number and an unsigned number on a numeric value.

The highest bit of the signed number is used to represent the symbol, so the maximum number of symbols is less than the unsigned number on the maximum value. Take one byte as an example:

The number of signed values range from: -128-0-127

The value range for unsigned numbers is: 0-255


2: Conversion of positive and negative numbers

The conversion relationship is: negative (positive) = positive (negative) complement + 1;

For example:

5 = 0000 0101

-5 = 1111 1011

The actual calculation:

Maximum value-current value +1;

0xff-5 +1 = 5 (1111 1011)

0xFF-(-5) +1 = 5 (0000 0101)


3: Positive negative numbers are stored in the computer

In the computer, there is no so-called positive or negative, specifically see the following code

int main (void)
{
int x =-1;
int i = 0;
unsigned int ux = (unsigned) x;
for (i = 0;i<32;i++)
{
UX = UX >> i;
if (UX & 0x01) = = 0)
printf ("%d = 1\r\n", i);
}

UX = (unsigned) x;

printf ("UX =%d \ n", UX);

printf ("UX =%u \ n", UX);

}

The result of the operation is:

111111111 (32 of 1)

UX =-1

UX = 4294967295

The reason is that when we assign 1 to the UX by forcing the type conversion, the address of the UX variable at that time, the value is 1, that is 0xFFFFFFFF, that is, from the storage point of view, 1 and 4294967295 in the computer storage value is 0xFFFFFFFF, The key is the way you interpret it,

UX =-1; At this point, we are going to parse the value of this storage space by%d, which is symbolic, so the result is-1;

UX = 4294967295, at this point we are in the way%u is unsigned shaping to parse the value of this storage space, so the highest bit is the value bit, not the sign bit.

The parsing process is as follows, assuming that the int type is one byte (4 bytes is the same principle, but the value is even larger)

Right 255
-1
-bit
Value
-bit
Value
1
1
1
1
1
2
1
2
1
2
4
1
4
1
4
8
1
8
1
8
16
1
16
1
16
32
1
32
1
32
64
1
64
1
64
128
1
128
1
-128

So

-1 = 1+2+4+8+16+32+64+ (-128)

255 = 1+2+4+8+16+32+64+128

In summary, the computer's storage method does not differentiate between positive and negative, the key is the programmer in what way to parse the value of the storage space (address).

This article is from the "11664570" blog, please be sure to keep this source http://11674570.blog.51cto.com/11664570/1956201

Signed number and unsigned number in C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.