Hi,
Could someone teach me how to calculate the range of 8-bits and 16-bits 2's complement numbers in binary, decimal and hex.
thanks
Printable View
Hi,
Could someone teach me how to calculate the range of 8-bits and 16-bits 2's complement numbers in binary, decimal and hex.
thanks
Hi!
Let me first explain the (unsigned) binary system of counting by just counting. Let's take a 8 bit integer (an "unsigned char"):
00000000 0
00000001 1
00000010 2
00000011 3
00000100 4
00000101 5
00000110 6
00000111 7
00001000 8
and so on...
01111111 127
10000000 128
and so forth...
11111110 254
11111111 255
and in particular (a^b means "a to the power b" here, not the C operator ^):
00000001 1 = 2^0
00000010 2 = 2^1
00000100 4 = 2^2
00001000 8 = 2^3
00010000 16 = 2^4
00100000 32 = 2^5
01000000 64 = 2^6
10000000 128 = 2^7
Signed integers behave a "sign bit". Most CPU's use the 2's complement variant. This is because it leaves addition and subtraction exacly the same operation. The most significant bit (leftmost in the usual notation) is the sign bit: 0 for positive values and 1 for negative values.
00000000 0
00000001 1
00000010 2
00000011 3
00000100 4
00000101 5
00000110 6
00000111 7
00001000 8
and so on...
01111111 127
10000000 -128
10000001 -127
and so forth...
11111110 -2
11111111 -1
From this example, we can see that the smallest number is -2^7 (10000000 binary, 80 hex) and the largest number is (2^7)-1 (01111111 binary, 7f hex). For 16 bit integers the range is -2^15 (1000000000000000 binary, 8000 hex) to (2^15)-1 (0111111111111111 binary, 7fff hex).
For a n-bit two's complement number, the range is given by -2^(n-1) to (2^(n-1))-1.
This should help...
alex
Just to add a little bit to alex's nice explanation:
The easiest way to make a binary number its negative version is to flip all the digits (1 -> 0 and 0 -> 1) and just add one.
00010100 = 20
:flip:
11101011
+ 1
-------------
11101100 = -20
edit/addendum 1:
Also, to go from binary to hex, chop the number into groups of 4.
For example:
11010100 = 212 (base 10)
:split it:
1101 = 13 = D
0100 = 4 = 4
So, D4 = 212
For octal, do the same thing but split it into groups of 3.
edit/addendum 2:
Looking at the actual question, I just realized alex answered it perfectly and I just went nowhere fast :) Oh well, I'll just leave this up in case anyone was having trouble converting binary to hex or octal :o
And how do we print the binary of say, a number or a char, or a string? %b? hehe
something like this seba...
Code:#include<stdio.h>
void printbits(unsigned int tobeprinted)
{
unsigned int shift=8*sizeof(unsigned int)-1; // bits are 0-31 not 1-32
unsigned int mask=1<<shift;
printf("%i is ",tobeprinted);
for(unsigned int i=1;i<=(shift+1);i++)
{
if(tobeprinted &mask) printf("1");
else
printf("0");
tobeprinted <<= 1;
if (i%8==0) printf(" ");
}
}
int main()
{
for (int i=1;i<10;i++)
{
printf("Enter positive integer :-");
unsigned int input;
scanf("%i",&input);
printbits(input);
printf("\n");
}
return 0;
}
Here's the lazy programmer's method.Quote:
Originally posted by Sebastiani
And how do we print the binary of say, a number or a char, or a string? %b? hehe
Code:int num = 32767;
char str[33];
itoa(num,str,2);
printf("number in binary:%s\n",str);