Object |
Unsigned Range | Signed Range |
Bits |
Hex Digits (4 bits) | Bytes (8 bits) | Octal Digits (3 bits) |
Bit |
0..1 |
-1..0 |
1 |
less than 1 | less than 1 | less than 1 |
Byte, "char" |
255 |
-128 .. 127 |
8 |
2 | 1 | two and two thirds |
"short" (or Windows WORD) |
65535 |
-32768 .. +32767 |
16 |
4 | 2 | five and one third |
"int" (Windows DWORD) |
>4 billion |
-2G .. +2G |
32 |
8 | 4 | ten and two thirds |
"long" (or "long long" on some machines) |
>16 quadrillion |
-8Q .. +8Q |
64 |
16 | 8 | twenty-one and one-third |
int value=1; /* value to test, starts at first (lowest) bit */
for (int bit=0;bit<100;bit++) {
std::cout<<"at bit "<<bit<<" the value is "<<value<<"\n";
value=value+value; /* moves over by one bit (value=value<<1 would work too) */
}
return 0;
/* Print the low 32 bits of this number */(executable NetRun link)
void print_binary(int v)
{
for (int bit=31;bit>=0;bit--) {
if ((v&(1<<bit))!=0) {
std::cout<<"1";
} else {
std::cout<<"0";
}
}
std::cout<<" binary\n";
}
int foo(void) {
print_binary(6);
return 0;
}
const int bits[]={1,1,0};
int nbits=sizeof(bits)/sizeof(bits[0]); /*<- funky trick to find size of static array!*/
int value=0;
for (int bit=0;bit<nbits;bit++) {
if (bits[nbits-1-bit]) /*<- gotta index the bit array starting at the end */
value = value | (1<<bit);
}
return value;
Or you can do the same binary-to-integer conversion using digits from cin:
void print_binary(int v) {
for (int i=0;i<32;i++)
{
int mask=1<<(32-1-i);
if ((v&mask)==0) std::cout<<"0";
else std::cout<<"1";
}
std::cout<<endl;
}
int read_binary(void) {
int v=0;
while (std::cin) {
char c='?';
std::cin>>c;
//std::cout<<"I just read the character '"<<c<<"'."<<endl;
if (c=='0') {
/* zero digit in that place */
v=v<<1;
} else if (c=='1') {
/* one digit in that place */
v=(v<<1)+1;
}
else {
break;
}
}
return v;
}
int foo(void) {
return read_binary();
}
Place/bit Number | i | ... | 4 | 3 | 2 | 1 | 0 |
Decimal: Base-10 |
10^{i} |
... |
10000 |
1000 |
100 |
10 |
1 |
Binary: Base-2 |
2^{i} | ... |
16 = 2^{4} |
8 = 2^{3} |
4 = 2^{2} |
2 |
1 |
Octal: Base-8 |
8^{i} |
... |
4096=8^{4} | 512=8^{3} | 64=8^{2} | 8 |
1 |
Hex: Base-16 |
16^{i} | ... |
65536 = 2 |
4096 = 16^{3} | 256 = 16^{2} |
16 |
1 |
Base-n |
n^{i} | ... |
n^{4} |
n^{3} | n^{2} | n |
1 = n^{0} |
Decimal |
Hex | Binary |
0 |
0 | 0 |
1 |
1 | 1 |
2 |
2 | 10 |
3 |
3 | 11 |
4 |
4 | 100 |
5 |
5 | 101 |
6 |
6 | 110 |
7 |
7 | 111 |
8 |
8 | 1000 |
9 |
9 | 1001 |
10 |
A | 1010 |
11 |
B | 1011 |
12 |
C | 1100 |
13 |
D | 1101 |
14 |
E | 1110 |
15 |
F | 1111 |
16 |
10 |
10000 |
Hex Place-Value |
16^{3} |
16^{2} |
16 |
1=16^{0} |
||||||||||||
Hex Digit |
F |
0 |
3 |
6 |
||||||||||||
Binary Digit |
1 |
1 |
1 |
1 |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
1 |
0 |
1 |
1 |
0 |
Binary Place-Value |
2^{15} |
2^{14} |
2^{13} |
2^{12} |
2^{11} |
2^{10} |
2^{9} |
2^{8} |
2^{7} |
2^{6} |
2^{5} |
2^{4} |
2^{3} |
2^{2} |
2^{1} |
2^{0} |
804a516: 80 fa 3f cmp dl,0x3fNote that every single number is listed in hex--the addresses, on the left; the machine code, in the middle; and the constants in the assembly, on the right. A binary file display tool is called a "hex dump". A binary file editor is called a "hex editor". That's how common hex is, so for the rest of the class to make sense, you've gotta learn it!
804a519: 0f 84 7c 01 00 00 je 804a69b <exit@plt+0xc2b>
int v=1024+15;
for (int digit=7;digit>=0;digit--) {
char *digitTable="0123456789abcdef";
int d=(v>>(digit*4))&0xF;
std::cout<<digitTable[d];
}
std::cout<<std::endl;
return v;