To Documents
Binary and Hex Numbers
Inside a computer, integer values are stored as binary numbers.
Integer values are usually input into a computer as decimal numbers but, as we will see below,
these values can also be input in hex (base 16) or binary (base 2).
In the explanations that follows, an means a to the nth power (multiply a by itself n times).
- Converting from Binary to Decimal
First memorize this
Powers of Two table,
up to 27 = 128.
In IT 212, we will only be working with one byte integers with powers of two up to 128.
Example 1: Convert 00010110 (binary) to decimal.
Solution: Each binary digit (called a bit) represents a
power of two:
Power |
7 | 6 | 5 | 4 |
3 | 2 | 1 | 0 |
Power of Two |
128 | 64 | 32 | 16 |
8 | 4 | 2 | 1 |
Digit |
0 | 0 | 0 | 1 |
0 | 1 | 1 | 0 |
Therefore, the value in decimal is
00010110 = 0*27 + 0*26 + 0*25 + 1*24 +
0*23 + 1*22 +
1*21 + 0*20
= 0*128 + 0*64 + 0*32 + 1*16 + 0*8 + 1*4 + 1*2 + 0*1
= 16 + 4 + 2 = 22.
- Converting from Decimal to Binary
Example 2: Convert 22 (decimal) to binary.
Solution: Repeatedly subtract the largest power of two that
can be subtracted, starting with 22.
Power
of 2
22
- 16 4
--
6
- 4 2
--
2
- 2 1
--
0
Therefore 22 can be written as 22 = 16 + 4 + 2. Then
22 = 16 + 4 + 2
= 0*128 + 0*64 + 0*32 + 1*16 + 0*8 +
1*4 + 1*2 + 0*1
= 00010110
This binary value is written
in Python as 0b00010110.
- Converting from Hex to Binary
First memorize this Hex Digits table:
Then for each hex digit, replace it by the 4 bits in
the binary column.
Example 3: Convert 6d (hex) to binary:
Solution: 6 → 0110 and d → 1101, so 6d → 01101101.
- Converting from Binary to Hex
Group the binary bits into groups of 4. Then translate
each group of 4 bits (called a nybble!) into hex. Each
nybble is equivalent to one hex digit.
Example 4: Convert 01101101 (binary) to hex:
Solution: 01101101
0110 1101
6 d
6d.
The hex value 6d is written
in Python as 0x6d.
- Using Bitwise Operators
A mask is a binary number that can be used together with bitwise
operators (&, |, and
^) to manipulate binary data. Binary data is usually expressed
in hex because converting between hex and binary is very
easy. Here are some bitwise operators and Python code that manipulates them:
- Bitwise Or
The symbol for bitwise or is vertical line ( | ).
Here is the operation table for bitwise or:
Op 1 | Op 2 | Result |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Example 5: Compute the bitwise or of 0x6b and
0x52.
Hex Binary
==== ========
0x6B ---> 01101011
0x52 ---> 01010010
--------
0x7b <--- 01111011
Python code that performs this computation:
a = 0x6b
b = 0x52
c = a | b
print("{0:02x}".format(c))
- Bitwise And
The symbol for bitwise and is ampersand ( & ).
Here is the operation table for bitwise and:
Op 1 | Op 2 | Result |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Example 6: Compute the bitwise and of 0x6B and
0x52.
Hex Binary
==== ========
0x6B ---> 01101011
0x52 ---> 01010010
--------
0x42 <--- 01000010
Python code that performs this computation:
a = 0x6b
b = 0x52
c = a & b
print("{0:02x}".format(c))
- Bitwise Exclusive Or
The symbol for bitwise exclusive or (XOr) is caret ( ^ ).
The operation table for bitwise exclusive or:
Op 1 | Op 2 | Result |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Example 7: Compute the bitwise and of 0x6B and 0x52.
Hex Binary
==== ========
0x6B ---> 01101011
0x52 ---> 01010010
--------
0x39 <--- 00111001
Python code that performs this computation:
a = 0x6b
b = 0x52
c = a ^ b
print("{0:02x}".format(c))
- Uses for Bitmaps
A bitmap is a sequence of bits stored in one or more binary integers.
Here are some of the uses of bitmaps: to store the
- locations of deleted items in an array
- locations of changed items in an array
(the bits in such a bitmap are called dirty bits)
- answers for a true/false questionaire
- status of records in a database (locked or not locked)
- status of mouse buttons (pressed or not pressed)
- status of the Shift, Control, or Alt keys (pressed or not pressed)
- status of a font in a word processor (Bold/Not Bold, Italic/Not Italic,
- Underlined/Not Underlined)
- status of books in a library (checked out or not checked out).
- Using Binary Masks to Manipulate Data
- Use a bitwise or mask (|) to set (turn on orchange to 1) bits in a
binary integer. In Section 5a, the mask 0x52 (binary 01010010)
turns on bits numbered 2, 4, and 7 in the integer 0x6B.
- Use a bitwise and mask (&) to clear (turn off or change to 0) bits in a
binary integer. In Section 5b, the mask 0x52 (binary 01010010)
turns off bits numbered 1, 3, 5, 6, 8 in the integer 0x6B.
- Use a bitwise exclusive or mask (^) to toggle (change from 0
to 1 or from 1 to 0) bits in a binary integer. In Section 5c,
the mask 0x52 (binary 01010010) toggles bits numbered 2, 4,
and 7 in the integer 0x6b.
- Representing Negative Numbers in Binary
Binary negative numbers are represented in twos-complement notation.
An explicit negative sign is not used. Use this
algorithm to represent the negative number -a in binary:
Twos-complement Algorithm 1 (Decimal to Binary)
- Convert the positive number +a to the binary representation b.
- Complement the binary representation of b (change each 1 to 0 and each 0 to 1).
- Searching from the right, find the first 0 bit.
- Change this 0 bit to 1.
- Change all of the 1 bits to the right of the newly changed bit to 0.
Example: Represent -48 in twos-complement notation.
- Convert +48 to binary: 00110000.
- Complement 00110000 → 11001111.
- The first 0 bit searching from the right is marked in red:
11001111. Change it to 1:
11011111.
- Change all the bits to the left of the red bit to 0:
11010000.
This answer of 11010000 can also be written in hex: d0.
Note: If the binary representation of a number starts with 0, the
number is positive. If the binary representation starts with 1,
the number is negative.
Use the following algorithm to convert a negative binary number b to
decimal:
Twos-complement Algorithm 2 (Binary to Decimal)
- Complement the binary number b.
- Searching from the right, find the first 0 bit.
- Change this 0 bit to 1.
- Change all of the 1 bits to the right of the newly changed bit to 0.
- Convert the resulting binary number to the decimal number a.
- The original negative binary number represents -a.
Example: Convert the negative binary number 11110100 to decimal.
- Complement 11110100 → 00001011.
- The first 0 bit searching from the right is marked in red:
00001011. Change it to 1:
00001111.
- Change all the bits to the left of the red bit to 0:
00001100.
- Convert 00001100 to decimal: 12.
- The original negative binary number 11110100 is -12 in binary.