Nerveware
Bitwise calculations |
Prev |
Created by Bayou. |
Next |
Binary numbers
Before I explain binary numbers, I probably should refresh your
memory about what decimal numbers are. Decimal numbers are numbers which
exist in the decimal system. For example, we have the decimal number
138. This number exists from one hundred, three tens and eight ones.
Or written uniform:
The binary numbers share the same concept, but on a base-2 numeral scale.
The notation NR2 will be used throughout the document to mark
such numbers. For decimal numbers, the NR10 notation will be
used. In computer language, binary numbers are prefixed with 0b.
The number 138R10 is written different in the binary system.
Writing 138R2 makes no sense. Decimal numbers
consist from zeros to nines, binary numbers consist from zeros and ones.
As you may expect, 138R10 is should be written down as
follows:
27 |
26 |
25 |
24 |
23 |
22 |
21 |
20 |
1 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
Assume we want to multiply this number by 2R10. One gets the
following formula.
27 |
26 |
25 |
24 |
23 |
22 |
21 |
20 |
|
1 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
|
0 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
* |
If we would not stretch the table, the number overflows. An overflow
happens whenever the number is too small for the table and data will be
lost because of it.
Wrong: overflow |
27 |
26 |
25 |
24 |
23 |
22 |
21 |
20 |
|
1 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
|
0 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
* |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
0 |
|
Correct: no overflow |
28 |
27 |
26 |
25 |
24 |
23 |
22 |
21 |
20 |
|
0 |
1 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
|
0 |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
* |
1 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
0 |
|
Coming up: Bitwise Operators. 01111001 01100101 01100001!