4 - Binary Numbers
4 - Binary Numbers
Representations
1
Readings and Exercises
• P & H: Section 2.4
2
Objectives
At the end of this section, you will be able to
1. Represent numbers in binary
2. Represent negative binary numbers
3. Use the hexadecimal number system
4. Use the Octal number system
3
Binary Numbers
• Are base 2 numbers
• Use only the binary digits (bits) 0 and 1
• Easy to encode on a computer, since only 2 states
need to be distinguished
▪ Using voltages:
• 0: 0 V
• 1: 3.3 V
4
Binary Numbers (cont’d)
▪ Using paper tape or cards:
• 0: unpunched
• 1: punched
▪ Using toggle switches or bulbs
• 0: off
• 1: on
• Binary numbers are encoded using 1 or more bits
in combination
▪ Eg: 101 binary is decimal 5
5
Binary Numbers (cont’d)
• An N-bit register can hold 2N bit patterns
6
Unsigned Integers
• Are encoded using binary numbers
▪ Range: 0 to 2N – 1, where N is the number of bits
▪ Eg: 8-bit register: ranges from 0 to 255
• In binary, from 00000000 to 11111111
7
Signed Integers
• Are most commonly encoded using the two’s
complement representation
▪ Range: -2N-1 to +2N-1 – 1
• 4-bits: -8 to +7
• 8-bits: -128 to +127
• 16-bits: -32,768 to + 32,767
• 32-bits: -2,147,483,648 to +2,147,483,647
• 64-bits: -263 to +263 – 1
8
Signed Integers (cont’d)
• Negating a number is done by:
1. Taking the one’s complement
• Toggle all 0’s to1’s, and vice versa
2. Adding 1 to the result
▪ Eg: find the bit pattern for -5 in a 4-bit register
• +5 is 0101
• One’s complement: 1010
• Add 1: 1011
9
Signed Integers (cont’d)
• Also works when negating negative numbers
▪ Eg: -5 to +5
• One’s complement of 1011: 0100
• Add 1: 0101
• All positive numbers will have a 0 in the left-
most bit
▪ And all negatives will have a 1
▪ Called the sign bit
10
Signed Integers (cont’d)
• The sign-magnitude and one’s complement
representations are also possible
▪ But are awkward to handle in hardware
▪ Have two zeroes: +0 and -0
▪ Rarely used today
11
Hexadecimal Numbers
• Are base 16 numbers
• Use the digits: 0, 1, 2, . . . , 9, A, B, C, D, E, F
• Are commonly used as a shorthand for denoting
bit patterns
12
Hexadecimal Numbers (cont’d)
Each hex digit corresponds to a particular 4-bit pattern:
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111
13
Hexadecimal Numbers (cont’d)
▪ Eg: 0xF5A
1111 0101 1010
14
Octal Numbers
• Are base 8 numbers
• Use the digits: 0, 1, 2, . . ., 7
• May be used as a shorthand for denoting bit
patterns
▪ Each digit corresponds to a 3-bit pattern
▪ Eg: 0756
111 101 110
15
Integer Classes and Subtypes
• Linux on ARMv8 in AArch64 uses the LP64 data
model
▪ long ints and pointers are 64-bits long
16
Integer Classes and Subtypes (cont’d)
▪ In C, use the “unsigned” keyword to denote unsigned
integers
• Eg: unsigned int x; // 32 bits
• Eg: unsigned char y; // 8 bits
17