So
here's normal, base-1 counting on your fingers. In base 1, you
just raise the number of fingers equal to the value you're trying to
represent:
|
|
This is funky base-2 counting on your fingers. Each finger represents a
different value now, so you have to start counting with '1' at your
pinky, then '2' with just your ring finger, and '3=2+1' is pinky and
ring finger together. '4' is a single raised middle finger. Then
'5=4+1' is middle finger and pinky, and so on. Just 10 digits actually
allows you to count all the way to 1023, but we'll ignore the thumbs
and just use 8 fingers, to count up to 255=128+64+32+16 (left hand
palm-up, pinky is 16) +8+4+2+1 (right hand palm-down, pinky is 1).
This is actually somewhat useful for counting--try it! (Note: the numbers four, sixty-four, and especially sixty-eight should not be prominently displayed. Digital binary counting is not recommended in a gang-infested area.) |
long x=1024;Why? The real answer is 4 billion (and change), which requires 33 bits: a 1 followed by 32 zero bits. But on a 32-bit machine, all you get is the zeros; the higher bits "overflow" and (at least in C/C++) are lost! Understanding the bits underneath your familiar integers can help you understand errors like this one. (Plus, by writing assembly code, you can actually recover the high-order bits after a multiplication if you need them.)
long y=x*x*x*4;
return y;
Place/bit Number | i | ... | 4 | 3 | 2 | 1 | 0 |
Decimal: Base-10 |
10i |
... |
10000 |
1000 |
100 |
10 |
1 |
Binary: Base-2 |
2i | ... |
16 = 24 |
8 = 23 |
4 = 22 |
2 |
1 |
Octal: Base-8 |
8i |
... |
4096=84 | 512=83 | 64=82 | 8 |
1 |
Hex: Base-16 |
16i | ... |
65536 = 2 |
4096 = 163 | 256 = 162 |
16 |
1 |
Base-n |
ni | ... |
n4 |
n3 | n2 | n |
1 = n0 |
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 |
163 |
162 |
16 |
1=160 |
||||||||||||
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 |
215 |
214 |
213 |
212 |
211 |
210 |
29 |
28 |
27 |
26 |
25 |
24 |
23 |
22 |
21 |
20 |
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>