The Art of Assembly Language
2.6 Some Useful Properties of Binary Numbers
It's worth learning a few interesting facts about binary values that you might find useful in your programs. Here are some useful properties:
-
If bit position zero of a binary (integer) value contains one, the number is an odd number; if this bit contains zero, then the number is even.
-
If the LO n bits of a binary number all contain zero, then the number is evenly divisible by 2 n .
-
If a binary value contains a one in bit position n , and zeros everywhere else, then that number is equal to 2 n .
-
If a binary value contains all ones from bit position zero up to (but not including) bit position n , and all other bits are zero, then that value is equal to 2 n ˆ’ 1.
-
Shifting all the bits in a number to the left by one position multiplies the binary value by two.
-
Shifting all the bits of an unsigned binary number to the right by one position effectively divides that number by two (this does not apply to signed integer values). Odd numbers are rounded down.
-
Multiplying two n -bit binary values together may require as many as 2* n bits to hold the result.
-
Adding or subtracting two n -bit binary values never requires more than n +1 bits to hold the result.
-
Inverting all the bits in a binary number (that is, changing all the zeros to ones and all the ones to zeros) is the same thing as negating (changing the sign) of the value and then subtracting one from the result.
-
Incrementing (adding one to) the largest unsigned binary value for a given number of bits always produces a value of zero.
-
Decrementing (subtracting one from) zero always produces the largest unsigned binary value for a given number of bits.
-
An n -bit value provides 2 n unique combinations of those bits.
-
The value 2 n ˆ’ 1 contains n bits, each containing the value one.
You should probably memorize all the powers of two from 2 through 2 16 , as these values come up in programs all the time. Table 2-4 lists their values.
n | 2 n |
---|---|
| 1 |
1 | 2 |
2 | 4 |
3 | 8 |
4 | 16 |
5 | 32 |
6 | 64 |
7 | 128 |
8 | 256 |
9 | 512 |
10 | 1,024 |
11 | 2,048 |
12 | 4,096 |
13 | 8,192 |
14 | 16,384 |
15 | 32,768 |
16 | 65,536 |
Категории