Number Systems
Number Systems
Number Systems
NUMBER SYSTEMS
Each position represents a specific power of the base (10). For example, the decimal number
1234 consists of the digit 4 in the units position, 3 in the tens position, 2 in the hundreds position,
and 1 in the thousands position, and its value can be written as
Mrs. Onyango
ICS 2174: Introduction to Computer Science Number Systems and Conversions
Every binary number contains an infinite number of digits. Any number of leading zero bits may
precede the binary number without changing its value. For example, we can represent the
number five by:
101
00000101
0000000000101
000000000000101
To make it easier to read and comprehend, a convention is developed where the binary bits are
grouped in fours with a space separating the groups. For example, the binary value
1010111110110010 will be written as 1010 1111 1011 0010.
A big problem with the binary system is lack of compactness. Hexadecimal numbers offer the two
features we’re looking for: they’re very compact, and it’s simple to convert them to binary and
vice versa.
Since the radix (base) of a hexadecimal number is 16, each hexadecimal digit to the left of the
hexadecimal point represents some value times a successive power of 16. For example, the
number 123416 is equal to:
Mrs. Onyango
ICS 2174: Introduction to Computer Science Number Systems and Conversions
This is a base eight number system that utilizes the digits 0,1,2,3,4,5,6,7. Each position in an octal
number represents a certain power of the base (8). As in the other number systems, the bit zero
is the least significant bit (LSB) and bit 7 is the most significant bit (MSB).
Example
Octal Number: 125708
Mrs. Onyango
ICS 2174: Introduction to Computer Science Number Systems and Conversions
NUMBER CONVERSIONS
Binary to Decimal Conversion
To convert a binary number to decimal, each “1” in the binary string is multiplied by 2n where “n”
is the zero-based position of the binary digit. For example, the binary value 11001010 2
represents:
1*27 + 1*26 + 0*25 + 0*24 + 1*23 + 0*22 + 1*21 + 0*20
= 128 + 64 + 8 + 2
=20210
Decimal to Binary Conversion
To convert a decimal number into a binary number, divide through by two writing the remainder
in every division. The bits are then arranged from down to top.
Hexadecimal to Decimal Conversion
Multiply each digit by a power of 16 relative to the bit position.
Example:
19FDE16 = ((1 x 164) + (9 x 163) + (F x 162) + (D x 161) + (E x 160)) 10
= (65536+ 36864 + 3840 + 208 + 14)10
= 10646210
Hexadecimal to Binary Conversion
To convert a hexadecimal number into a binary number, simply substitute the corresponding four
bits for each hexadecimal digit in the number. For example, to convert 0ABCDh into a binary
value, simply convert each hexadecimal digit according to the table above:
0 A B C D Hexadecimal
0000 1010 1011 1100 1101 Binary
Binary to Hexadecimal Conversion
To convert a binary number into hexadecimal format is almost as easy. The first step is to pad the
binary number with zeros to make sure that there is a multiple of four bits in the number. For
example, given the binary number 1011001010, the first step would be to add two bits to the left
of the number so that it contains 12 bits. The binary value to be converted is 001011001010. The
next step is to separate the binary value into groups of four bits, i.e. 0010 1100 1010. Finally, look
Mrs. Onyango
ICS 2174: Introduction to Computer Science Number Systems and Conversions
up these binary values in the table above and substitute the appropriate hexadecimal digits,
giving 2CA.
Octal to Decimal Conversion
To convert from octal to decimal, multiply each digit by a power of 8 relative to its position.
125708= ((1 x 84) + (2 x 83) + (5 x 82) + (7 x 81) + (0 x 80)) 10
= (4096 + 1024 + 320 + 56 + 0)10
=549610
Decimal to Octal conversion
Divide through by 8 writing the remainder as in the decimal to binary conversion.
Mrs. Onyango
ICS 2174: Introduction to Computer Science Number Systems and Conversions
There are four main logical operations we’ll need to perform on binary digits: AND, OR, XOR
(exclusive-or), and NOT.
AND operation
The AND operation gives the following results:
0 and 0 = 0
0 and 1 = 0
1 and 0 = 0
1 and 1 = 1
It is represented by the truth table:
AND 0 1
0 0 0
1 0 1
“If the first operand is one and the second operand is one, the result is one; otherwise the result
is zero.”
OR operation
It is defined by: 0 or 0 = 0
0 or 1 = 1
1 or 0 = 1
1 or 1 = 1
The truth table for the OR operation takes the following form:
OR 0 1
0 0 1
1 1 1
Mrs. Onyango
ICS 2174: Introduction to Computer Science Number Systems and Conversions
“If the first operand or the second operand (or both) is one, the result is one; otherwise the result
is zero.”
XOR operation
It is defined as:
0 xor 0 = 0
0 xor 1 = 1
1 xor 0 = 1
1 xor 1 = 0
The truth table for the XOR operation takes the following form:
XOR 0 1
0 0 1
1 1 0
“If the first operand or the second operand, but not both, is one, the result is one; otherwise the
result is zero.”
“If one of the operands to the logical exclusive-OR operation is a one, the result is always the
inverse of the other operand.”
NOT operation
The logical NOT operation accepts only one operand while all other logical operations accept two:
NOT 0 = 1
NOT 1 = 0
The truth table is given by:
NOT 0 1
1 0
As described in the previous section, the logical functions work only with single bit operands.
Since the processor uses groups of eight, sixteen, or thirty-two bits, we need to extend the
Mrs. Onyango
ICS 2174: Introduction to Computer Science Number Systems and Conversions
definition of these functions to deal with more than two bits. Logical functions on the 80x86
operate on a bit-by-bit (or bitwise) basis. Given two values, these functions operate on bit zero
producing bit zero of the result. They operate on bit one of the input values producing bit one of
the result, etc. For example, if you want to compute the logical AND of the following two eight-
bit numbers, you would perform the logical AND operation on each column independently of the
others:
1011 0101
1110 1110
---------
1010 0100
This bit-by-bit form of execution can be easily applied to the other logical operations as well.
Since we’ve defined logical operations in terms of binary values, you’ll find it much easier to
perform logical operations on binary values than on values in other bases. Therefore, if you want
to perform a logical operation on two hexadecimal numbers, you should convert them to binary
first. This applies to most of the basic logical operations on binary numbers (e.g., AND, OR, XOR,
etc.).
The ability to force bits to zero or one using the logical AND/OR operations and the ability to
invert bits using the logical XOR operation is very important when working with strings of bits
(e.g., binary numbers). These operations let you selectively manipulate certain bits within some
value while leaving other bits unaffected. For example, if you have an eight-bit binary value ‘X’
and you want to guarantee that bits four through seven contain zeros, you could logically AND
the value ‘X’ with the binary value 0000 1111. This bitwise logical AND operation would force the
H.O. four bits to zero and pass the L.O. four bits of ‘X’ through unchanged. Likewise, you could
force the L.O. bit of ‘X’ to one and invert bit number two of ‘X’ by logically ORing ‘X’ with 0000
0001 and logically exclusive- ORing ‘X’ with 0000 0100, respectively. Using the logical AND, OR,
and XOR operations to manipulate bit strings in this fashion is known as masking bit strings. We
use the term masking because we can use certain values (one for AND, zero for OR/XOR) to ‘mask
out’ certain bits from the operation when forcing bits to zero, one, or their inverse.
Mrs. Onyango
ICS 2174: Introduction to Computer Science Number Systems and Conversions
Mrs. Onyango
ICS 2174: Introduction to Computer Science Number Systems and Conversions
Mrs. Onyango
ICS 2174: Introduction to Computer Science Number Systems and Conversions
BINARY ARITHMETIC
Examples:
0011 1011
+ 0111 1010
1011 0101
1011 1001
+ 0100 0101
1111 1110
1111 1001
- 0100 1000
1011 0001
1011 1001
- 0100 0101
01110100
Multiplication example
Division example
101 100101
Mrs. Onyango
ICS 2174: Introduction to Computer Science Number Systems and Conversions
Example: 510-210
000001012 +5
000000102 +2
+ 1
11111110
11111110 -2
00000101
+11111110
00000011 +3
Mrs. Onyango