Sie sind auf Seite 1von 19

TA C162 Computer Programming I

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

Two’s Complement Representation


If number is positive or zero
• Normal binary representation
If number is negative
• Start with positive number
• Flip every bit (i.e., take the one’s complement)
• Then add one
Example:
00101 (5) 01001 (9)
11010 (1’s comp) 10110 (1’s comp)
+ 1 + 1
11011 (-5) 10111 (-9)

13/01/2009 2
TA C162 Computer Programming I

Two’s Complement Shortcut


To take the two’s complement of a number:
• Copy bits from right to left until (and including) the first “1”
• Flip remaining bits to the left

011010000 011010000
100101111 (1’s comp) (flip) (copy)
+ 1
100110000 100110000

13/01/2009 3
TA C162 Computer Programming I

Advantage of 2’s complement


Two’s complement representation developed to make
circuits easy for arithmetic.
• For each positive number (X), assign value to its negative
(-X),
such that X + (-X) = 0 with “normal” addition, ignoring carry
out
00101 (5) 01001 (9)
+ 11011 (-5) + 10111 (-9)
00000 (0) 00000 (0)

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

Two’s Complement Signed Integers


MS bit is sign bit:– it has weight –2n-1.
Range of an n-bit number: -2n-1 through 2n-1 – 1.
• The most negative number (-2n-1) has no positive
counterpart.
23 22 21 20 23 22 21 20
0 0 0 0 0 1 0 0 0 -8
0 0 0 1 1 1 0 0 1 -7
0 0 1 0 2 1 0 1 0 -6
0 0 1 1 3 1 0 1 1 -5
0 1 0 0 4 1 1 0 0 -4
0 1 0 1 5 1 1 0 1 -3
0 1 1 0 6 1 1 1 0 -2
0 1 1 1 7 1 1 1 1 -1
13/01/2009 6
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

Converting Binary (2’s C) to Decimal


1. If leading bit is one, take two’s
complement to get a positive number.
n 2n
0 1
3. Add powers of 2 that have “1” in the
1 2
corresponding bit positions.
2 4
3 8
5. If original number was negative, 4 16
add a minus sign. 5 32
6 64
7 128
Assuming 8-bit 2’s complement numbers. 8 256
9 512
10 1024
13/01/2009 8
TA C162 Computer Programming I

Example: Binary to Decimal Conversion


X = 01101000two
Step 1: Leading bit is 0 no need to take 2’s complement
X = 01101000
Step 2: Add powers of 2 that have “1” in the corresponding bit
positions.
= 26+25+23 = 64+32+8
X = 104ten

Step 3: Not required (Number is positive)

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

Assuming 8-bit 2’s complement numbers.

13/01/2009 10
TA C162 Computer Programming I

Converting Decimal to Binary (2’s C)


First Method: Division
2. Find magnitude of decimal number. (Always +ive)
3. Divide by two – remainder is least significant bit.
4. Keep dividing by two until answer is zero, writing
remainders from right to left.
5. Append a zero as the MS bit.

If original number was negative, take two’s


complement.

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

Converting Decimal to Binary (2’s C) n 2n


Second Method: Subtract Powers of Two 0 1
1 2
2. Find magnitude of decimal number.
2 4
3. Subtract largest power of two less than or equal3 8
to number. 4 16
4. Put a 1 in the corresponding bit position. 5 32
6 64
5. Keep subtracting until result is zero. 7 128
6. Append a zero as MS bit; if original was 8 256
negative, take two’s complement. 9 512
10 1024
7. Put a 0 in the other bit positions

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

Question: How to convert a Binary number to its Octal


equivalent?
13/01/2009 16
TA C162 Computer Programming I

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

Binary Hex Decimal Binary Hex Decimal


0000 0 0 1000 8 8
0001 1 1 1001 9 9
0010 2 2 1010 A 10
0011 3 3 1011 B 11
0100 4 4 1100 C 12
0101 5 5 1101 D 13
0110 6 6 1110 E 14
0111 7 7 1111 F 15
13/01/2009 17
TA C162 Computer Programming I

Converting from Binary to Hexadecimal


Every four bits is a hex digit.
• Start grouping from right-hand side
Example:
011101010001111010011010111

3 A 8 F 4 D 7

This is not a new machine representation,


just a convenient way to write the number.

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

Das könnte Ihnen auch gefallen