Reputation: 61
I am going over some revision and I came across a question that asked what is 10011001 in signed integer and unsigned. I know the unsigned integer is 153 because there are no negatives in unsigned integers, but am I correct to say the signed integer of 10011001 is -153 or am I making a mistake ?
Upvotes: 1
Views: 379
Reputation: 11703
This difference between unsigned and signed number is that one of the bit is used to indicate positive or negative number.
So in your example you have 8 bits.
If I treat is as signed, then I have 7 bits to work with: 2^7
If I use all 8 bits then I unsigned bits to work with: 2^8
Here is code to demonstrate the answer:
char *endptr;
char binary[11] = "10011001"; // need an extra char for the termination
char x = (char)strtol(binary, &endptr, 2);
unsigned char y = (unsigned char)strtol(binary, &endptr, 2);
printf("%s to signed char (1 byte): %i\n", binary, (short)x);
printf("%s to unsigned char (1 byte): %u\n", binary, y);
Output:
Upvotes: 1