Binary Representation Digital Representations • Only two states (0 and 1)

advertisement
Digital Representations
Binary Representation
• Only two states (0 and 1)
• Easy to implement electronically
MSB
% 0 = (0)10
% 1 = (1)10
% 10 = (2)10
% 11 = (3)10
% 100 = (4)10
% 101 = (5)10
% 110
% 111
%1000
%1001
%1010
%1011
= (6)10
= (7)10
= (8)10
= (9)10
= (10)10
= (11)10
% 1100
% 1101
% 1110
% 1111
%10000
%10001
= (12)10
= (13)10
= (14)10
= (15)10
= (16)10
= (17)10
LSB
%
%
%
10010 = (18)10
10011 = (19)10
10100 = (20)10
Nibble
:
%10011001 = (153)10
:
Byte :
word = 16 bits (2 bytes)
long word = 32 bits (4 bytes)
(double)
ME 461
1
© 2013 Stephen R. Platt
Digital Representations
Hexidecimal (HEX) Representation
• Easier to work with than long strings of 1s and 0s
• 16 “digits” each representing 4 bits
$0 = %0000 = (0)10
$1 = %0001 = (1)10
$2 = %0010 = (2)10
$3 = %0011 = (3)10
$4 = %0100 = (4)10
$5 = %0101 = (5)10
$6 = %0110 = (6)10
$7 = %0111 = (7)10
ME 461
$8 = %1000 = (8)10
$9 = %1001 = (9)10
$A = %1010 = (10)10
$B = %1011 = (11)10
$C = %1100 = (12)10
$D = %1101 = (13)10
$E = %1110 = (14)10
$F = %1111 = (15)10
2
© 2013 Stephen R. Platt
Digital Representations
Hex to Binary Conversion
• Each Hex digit represents 4 bits
• Simply convert each digit to its 4-bit value
Example: Convert $DF3 to binary
$DF3 = %1101 1111 0011
ME 461
3
© 2013 Stephen R. Platt
Digital Representations
Binary to Hex Conversion
• Create groups of 4 bits (starting with LSB)
• Pad last group with zeros if needed
• Convert each group to corresponding Hex digit
Example: Convert %1101000101 to Hex
%1101000101 = %0011 0100 0101
3
4
5
%1101000101 = $345
ME 461
4
© 2013 Stephen R. Platt
Digital Representations
Fixed Precision
• Beware of overflow problems!
• Microprocessors limit numbers to a fixed number of bits:
For example: What is the result of 255 + 1 (assuming 8 bit precision)?
255 =
%11111111 = $FF
+ 1 = %00000001 = $01
------------------256  %00000000 = $00
$F + 1 = 0, carry
$F + 1 (carry) + 0 = 0, carry
Carry out of MSB falls off the end because there is no place to put it!
Final answer is WRONG because could not store carry bit.
ME 461
5
© 2013 Stephen R. Platt
Digital Representations
Signed Integers
• What do we do about negative numbers?
• For straight binary we can represent 2N (positive) numbers
• To handle positive and negative numbers, the sign is an extra
piece of information that must be encoded.
• Two’s Complement is the most common representation
• Two’s Complement
• MSB becomes the SIGN bit (1 indicates a negative number)
• Can now represent signed integers -2N-1 to +2N-1 – 1 (e.g., -128 to
127)
• Algorithm #1: complement binary number and then add 1
• Algorithm #2: start with LSB, copy bits up to and including the first 1,
then invert all remaining bits Important Note: The computer generally does
Example: 14 = %0000 1110
-14 = %1111 0010
ME 461
not know that a bit sequence represents a
signed number. It is the programmer’s (i.e.,
your) responsibility!
6
© 2013 Stephen R. Platt
Digital Representations
Signed Integers
• Does arithmetic still work?
Example: What is the sum of (-128)10 + (127)10 in binary representation?
(Verify by converting the result to decimal representation.)
Solution: (-128)10 = %10000000
+(127)10 = %01111111
%11111111 (in two’s complement)
MSB =1 tells you this
is a negative number
Complement and add 1 to get magnitude
%00000000 + %00000001 = %0000001 = (1)10
Final result is (-1)10, so life is good!
ME 461
7
© 2013 Stephen R. Platt
Digital Representations
Two’s Complement Overflow
• What happens if we do (1)10 + (127)10 using
two’s complement representation?
(1)10 = %00000001
+(127)10 = %01111111
%10000000 (in two’s complement)
MSB =1 tells you this
is a negative number
Complement and add 1 to get magnitude
%01111111 + %00000001 = %10000000 = (128)10
Final result is (-128)10, so life is not so good!
ME 461
8
© 2013 Stephen R. Platt
Digital Representations
Adding Precision (unsigned)
• What if we want to take an unsigned number and
add bits to it?
Just add zeros to the left!
(128)10 = $80
(8 bits)
= $0080
(16 bits)
= $00000080 (32 bits)
ME 461
9
© 2013 Stephen R. Platt
Digital Representations
Adding Precision (two’s complement)
• What if we want to take a two’s complement
number and add bits to it?
Take whatever the Sign bit is and extend it to the left.
(127)10 = $7F
= %01111111
(8 bits)
= $007F
= %0000000001111111 (16 bits)
= $0000007F= %00…………1111111 (32 bits)
(-128)10 = $80
= %10000000
(8 bits)
= $FF80
= %1111111110000000 (16 bits)
= $FFFFFF80 = %11……….10000000 (32 bits)
This is called Sign Extension.
ME 461
10
© 2013 Stephen R. Platt
Digital Representations
Integer Types
• int (or signed int) 16bits on MSP430
• unsigned int 16bits on MSP430
• char (or signed char) 8bits on MSP430
• unsigned char 8bits on MSP430
ME 461
11
© 2013 Stephen R. Platt
Download