Beruflich Dokumente
Kultur Dokumente
Today’s Agenda
Representation of Binary Numbers
• Unsigned, Signed Magnitude, 1’s Compliment Done !!!
• 2’s Complement
Base Conversion
Binary to Decimal and Decimal to Binary Done !!!
Decimal to Octal and Octal to Decimal
Octal to Binary and Binary to Octal
Binary to Hexadecimal and Hexadecimal to Binary
13/01/2009 1
TA C162 Computer Programming I
13/01/2009 2
TA C162 Computer Programming I
011010000 011010000
100101111 (1’s comp) (flip) (copy)
+ 1
100110000 100110000
13/01/2009 3
TA C162 Computer Programming I
13/01/2009 4
TA C162 Computer Programming I
Example:
2+(-3) =??
Express -3 in 2’s complement form:
+3 00011
1’s Complement of 3 11100
2’s Complement of 3 11101 i.e. -3
2 00010
+ +
-3 11101
11111
Look at MSB 1 So take 2’s complement again
00001
13/01/2009 5
TA C162 Computer Programming I
What is Base?
•Decimal Number System
• Base is 10
• All numbers are represented by 0 to 9
•Binary Number System
• Base is 2
• All numbers are represented by 0 and 1
•Octal Number System
• Base is 8
• All numbers are represented by 0 to 7
•Inference
• Maximum digit value in any number system is Base -1
13/01/2009 7
TA C162 Computer Programming I
13/01/2009 9
TA C162 Computer Programming I
More Examples
X = 00100111two
= 25+22+21+20 = 32+4+2+1 n 2n
0 1
X = 39ten 1 2
2 4
X = 11100110two 3 8
4 16
-X = 00011010 5 32
6 64
= 24+23+21 = 16+8+2 7 128
= 26ten 8 256
9 512
X = -26ten 1 1
0 024
13/01/2009 10
TA C162 Computer Programming I
13/01/2009 11
TA C162 Computer Programming I
Example:
X = 104ten
104/2 = 52 r 0 bit 0
52/2 = 26 r 0 bit 1
26/2 = 13 r 0 bit 2
13/2 = 06 r 1 bit 3
6/2 = 03 r 0 bit 4
3/2 = 01 r 1 bit 5
1/2 = 0 r 1 bit 6
X=01101000two
13/01/2009 12
TA C162 Computer Programming I
13/01/2009 13
TA C162 Computer Programming I
Example:
X = 104ten 104 – 64 = 40 bit 7
40 – 32 = 8 bit 6
8–8 = 0 bit 4
X = 01101000two
13/01/2009 14
TA C162 Computer Programming I
Decimal to Octal
1. Find magnitude of decimal number. (Always +ive)
2. Divide by Eight – remainder is least significant bit.
3. Keep dividing by Eight until answer is zero, writing
remainders from right to left.
Example: Convert 40410 to its octal equivalent.
Octal to Decimal
Sum the multiplication of each digit with its position
value
Example: Convert 6248 to its decimal equivalent.
6 x 82 + 2 x 81 + 4 x 80 = 40410
13/01/2009 15
TA C162 Computer Programming I
Octal to Binary
First Method:
First convert Octal number into its Decimal Equivalent
and then From Decimal to Binary.
Second Method:
Write 3-bit binary equivalent for each octal digit in the
given number. Resultant number will be binary
equivalent.
Example: Binary equivalent of 6248
110010100
Hexadecimal Notation
It is often convenient to write binary (base-2) numbers
as hexadecimal (base-16) numbers instead.
Why?
• Fewer digits -- four bits per hex digit
• Less error prone -- easy to corrupt long string of 1’s and 0’s
3 A 8 F 4 D 7
13/01/2009 18
TA C162 Computer Programming I
Exercise?
How we convert a Hexadecimal Number into Binary
Number?
Try these:
3. ADF6
4. DB69
5. CA36
6. 5678
7. DABC
13/01/2009 19