Signed and unsigned integers

+1 Sergiu Man · July 28, 2014
Hello! i'm new in this and now i'm learning the C language. I don't understand what does signed and unsigned integers. i was reading about int8_t bit signed integer or INTN_MAX Maximum positive number of the signed integer of bits N. If someone can explain me or if you have tutorial. Thx.

Post a Reply

Replies

Oldest  Newest  Rating
+2 Eugene Botma · July 28, 2014
I'm not very good with C, so I might be wrong (sorry if I am), but the difference is as follows. I am using a 1 byte int as an example. ints are usually 4 bytes.

unsigned int: all 8 bits are used to store the numeric value of the digit. so the maximum value of a single 1byte int is: 128+64+32 +16 +8+ 4+ 2+1=255, and the minimum is 0. In the even of an integer overflow error, an unsigned ints value will go from 255 to 0, and then proceed to count upwards again.

Singed Int: the last (left most) digit is used to specify whether a digit is positive or negative. the rest are used for numeric value. This means an unsigned singly byte int has a maximum value of 64+32+16+8+4+2+1=127, and a minimum value of -127. In the event of a integer overflow error, the value will go from 127 to 0, and then count downwards to -127. once -127 has been passed, it will again reset to 0 and count upwards.

to calculate the maximum value:

an unsigned integer of n bits:

Minimum value=0; maximum value = (2^n)-1

a signed integer of n bits:

Minimum value = -((2^(n-1))-1); Maximum value = (2^(n-1))-1.

Fore example: 4 byte int has 32 bits:

Max Unsigned: (2^32)-1= 4294967295

min and max of Signed: (2^31)-1: -2147483647 and 2147483647
  • 1

C

107,249 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator