Top Banner
Digital Representations ME 461 1 Binary Representation •Only two states (0 and 1) •Easy to implement electronically % 0 = (0) 10 % 1 = (1) 10 % 10 = (2) 10 % 11 = (3) 10 % 100 = (4) 10 % 101 = (5) 10 LSB MSB Nibble Byte word = 16 bits (2 bytes) long word = 32 bits (4 bytes) (double) % 110 = (6) 10 % 111 = (7) 10 %1000 = (8) 10 %1001 = (9) 10 %1010 = (10) 10 %1011 = (11) 10 % 1100 = (12) 10 % 1101 = (13) 10 % 1110 = (14) 10 % 1111 = (15) 10 %10000 = (16) 10 %10001 = (17) 10 % 10010 = (18) 10 % 10011 = (19) 10 % 10100 = (20) 10 : %10011001 = (153) 10 : : © 2013 Stephen R. Platt
11

Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=

Jan 18, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=

Digital Representations

ME 461 1

Binary Representation

•Only two states (0 and 1)•Easy to implement electronically

% 0 = (0)10

% 1 = (1)10

% 10 = (2)10

% 11 = (3)10

% 100 = (4)10 % 101 = (5)10

LSBMSB

Nibble

Byte

word = 16 bits (2 bytes)long word = 32 bits (4 bytes)(double)

% 110 = (6)10

% 111 = (7)10

%1000 = (8)10

%1001 = (9)10

%1010 = (10)10 %1011 = (11)10

% 1100 = (12)10

% 1101 = (13)10

% 1110 = (14)10

% 1111 = (15)10

%10000 = (16)10 %10001 = (17)10

% 10010 = (18)10

% 10011 = (19)10

% 10100 = (20)10

:%10011001 = (153)10

::

© 2013 Stephen R. Platt

Page 2: Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=

Digital Representations

ME 461 2

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

$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

© 2013 Stephen R. Platt

Page 3: Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=

Digital Representations

ME 461 3

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

© 2013 Stephen R. Platt

Page 4: Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=

Digital Representations

ME 461 4

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

© 2013 Stephen R. Platt

Page 5: Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=

Digital Representations

ME 461 5

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, carryCarry 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.

© 2013 Stephen R. Platt

Page 6: Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=

Digital Representations

ME 461 6

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 bitsExample: 14 = %0000 1110 -14 = %1111 0010

Important Note: The computer generally does not know that a bit sequence represents a signed number. It is the programmer’s (i.e., your) responsibility!

© 2013 Stephen R. Platt

Page 7: Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=

Digital Representations

ME 461 7

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!

© 2013 Stephen R. Platt

Page 8: Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=

Digital Representations

ME 461 8

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!

© 2013 Stephen R. Platt

Page 9: Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=

Digital Representations

ME 461 9

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)

© 2013 Stephen R. Platt

Page 10: Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=

Digital Representations

ME 461 10

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.

© 2013 Stephen R. Platt

Page 11: Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=

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

© 2013 Stephen R. PlattME 461 11