0% found this document useful (0 votes)
108 views7 pages

Integer Representation

1) Computers represent data using binary digits (bits) that can have one of two states: 0 or 1. Numbers are stored using binary representations like binary integers and floating point. 2) Integers are commonly stored using two's complement representation where the most significant bit indicates the sign and positive and negative numbers are represented by flipping bits. 3) Two's complement addition works the same for positive and negative numbers by ignoring overflow, while subtraction is performed by negating and adding the numbers.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views7 pages

Integer Representation

1) Computers represent data using binary digits (bits) that can have one of two states: 0 or 1. Numbers are stored using binary representations like binary integers and floating point. 2) Integers are commonly stored using two's complement representation where the most significant bit indicates the sign and positive and negative numbers are represented by flipping bits. 3) Two's complement addition works the same for positive and negative numbers by ignoring overflow, while subtraction is performed by negating and adding the numbers.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Number Representations

How do we represent data in a computer?


At the lowest level, a computer is an electronic machine.
works by y controlling g the flow of electrons

Number Representation
COMP375 Computer Organization and dA Architecture hit t

Easy to recognize two conditions:


1. presence of a voltage well call this state 1 2. absence of a voltage well call this state 0

Could base state on value of voltage, but control and detection circuits more complex.
compare turning on a light switch to measuring or regulating voltage

Computer is a binary digital system.


Digital system: Binary (base two) system:

What kinds of data do we need to represent?


Numbers signed, g unsigned, g integers, g floating point, complex, rational, irrational, Text characters, strings, Images pixels, colors, shapes, Sound Logical true, true false Instructions

finite number of symbols

has two states: 0 and 1

Basic unit of information is the binary digit, or bit.

COMP375

Number Representations

Integer Numbers
Integers are almost universally stored as bi binary numbers. b When you enter an integer from the keyboard, software converts the ASCII or Unicode characters to a binary integer integer.

Integers
Weighted positional notation
like decimal numbers: 329 3 is worth 300, because of its position, while 9 is only worth 9
most significant least significant

329
102 101 100

101
21

22

20

3x100 + 2x10 + 9x1 = 329

1x4 + 0x2 + 1x1 = 5

Binary Fractions
Each position is twice the value of the position iti t to th the right. i ht 23 8 1 22 4 0 21 2 1 20 1 0 . . . 2-1 2-2 2-3
+

Binary Arithmetic
Base-2 addition just like base-10
add from right to left, left propagating carry
carry

1/2 1/4 1/8 0 0 1

10010 1001 11011

10010 1011 11101

1111 1 10000

What is this number in decimal?

10111 + 111

COMP375

Number Representations

Negative Integers
Almost all systems for storing negative bi binary numbers b set t th the l left ft most t bit (MSB) to indicate the sign of a number.

Signed Magnitude
Negative number are the same as positive g bit set with the sign
Three bit example

000 001 010 011 100 101 110 111 0 1 2 3 -0 -1 -2 -3

Common formats:
Signed Si d Magnitude M it d Ones Complement Twos Complement There There are t two o zeroes, eroes positi positive e and negati negative. e

Signed Magnitude Arithmetic


Normal addition does not work for negative ti signed i d magnitude it d numbers. b 1001 + 0100 1101 (-5) -1 +4 3

Ones Complement
Negative number are the logical inverse of positive numbers p
Three bit example

000 001 010 011 100 101 110 111 0 1 2 3 -3 -2 -1 -0

Mathematically positive and negative zero are the same, but they are different bit patterns.

COMP375

Number Representations

Ones Complement Arithmetic


The carry out of the sign position needs to b added be dd d t to th the number b
carry

Twos Complement
Negative number are the logical inverse of positive numbers p p plus 1.
Three bit example

000 001 010 011 100 101 110 111 -2 + -1 add carry out of sign -3 0 1 2 3 -4 -3 -2 -1

101 +110 011 +1 100

Normal binary arithmetic works for positive and negative numbers.

Twos Complement Representation


If number is positive or zero,
normal binary representation, zero in upper bit

Twos Complement Shortcut


To take the twos complement of a number: copy bits bit f from right i ht t to l left ft up to t and d including i l di the first 1 bit flip remaining bits to the left 011010000 100101111 1 100110000 011010000
(1 comp) (1s )

If number is negative,
start with positive number flip every bit (i.e., take the ones complement) then add one

00101 11010 + 1 11011

(5) (1s comp)

(-5)

01010 10101 + 1 10110

(10) (1s comp) (-10)

(flip)

(copy)

100110000

COMP375

Number Representations

Twos Complement Signed Integers


23 0 0 0 0 0 0 0 0 22 0 0 0 0 1 1 1 1 21 0 0 1 1 0 0 1 1 20 0 1 0 1 0 1 0 1 0 1 2 3 4 5 6 7 23 1 1 1 1 1 1 1 1 22 0 0 0 0 1 1 1 1 21 0 0 1 1 0 0 1 1 20 0 1 0 1 0 1 0 1 -8 -7 -6 -5 -4 -3 -2 -1

What is the decimal value of the 6 bit twos complement number?


20% 20% 20% 20% 20%

110011
A. -3 B. 33 C -11 C. 11 D. -12 E. -13
33 3 1 1 1 2 1 3

2s Complement Addition
Use normal binary addition regardless of sign. Ignore carry out
01101000 11110000 01011000
(104) (-16) (98)

Subtraction
Negate subtrahend (2nd number) and add. 00001000 - 00000101 00001000 +11111011 00000011 8 -5 8 + -5 3

11110110 + 11110111 11101101

(-10) (-9) (-19)

Assuming 8-bit 2s complement numbers.

Assuming 8-bit 2s complement numbers.

COMP375

Number Representations

Sign Extension
When moving an integer into a larger register the upper bits must be set to the sign bit. If we just pad with zeroes on the left:
4-bit 0100 (4) 1100 (-4) 8-bit 00000100 00001100

Overflow
If operands are too big, then sum cannot be represented as an n-bit 2s complement number.
carry into sign bit carry out of sign bit

(still 4) (12, not -4)

01000 + 01001 10001

(8) (9) (-15)

11000 + 10111 01111

(-8) (-9) (+15)

We have overflow if:


Instead, replicate the MS bit -- the sign bit:
4-bit 0100 (4) 1100 (-4) 8-bit 00000100 11111100

signs of both operands are the same same, and sign of sum is different.

(still 4) (still -4)

Another test -- easy for hardware:


carry into left most bit does not equal carry out

What is printed by this program?


int num; num = 100000000; while (num > 0) { num = num + 100000000; } System.out.println(num);
25% 25% 25% 25%

Decimal Numbers
Some systems (i.e. Intel Pentium) support decimal packed or unpacked p format. numbers in p packed decimal uses 4 bit fields for each digit 9375 = 1001,0011,0111,0101 unpacked decimal uses a byte per digit (ASCII) 9375 = 00111001,00110011,00110111,00110101

1. 2. 3. 4.

Runs forever zero large negative number -1

la ze rg e ro ne ga tiv e nu m be r

R un sf or ev er

COMP375

Number Representations

Number Order
With normal binary numbers, b many bit bits may change from number to number as you count up. 000 001 010 011 100 101 110 111

bits changed 1 2 1 3 1 2 1

Gray Code
With gray code numbers, b only l 1 bit changes from number to number as you count up. 000 001 011 010 110 111 101 100

bits changed 1 1 1 1 1 1 1

Gray Code Input


When reading sensors to t measure the position of something, gray codes reduce the probability of incorrect input.

Counting in Gray Code


T To make k the h li list twice as long, copy and reverse the list and pp a 1 bit to append the left.

COMP375

You might also like