0% found this document useful (0 votes)
13 views27 pages

5.negative Integers and Real Numbers

Uploaded by

cgeegegeg
Copyright
© © All Rights Reserved
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)
13 views27 pages

5.negative Integers and Real Numbers

Uploaded by

cgeegegeg
Copyright
© © All Rights Reserved
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/ 27

Data Representation

How Do Computers Store Data?



Remember when we said that it all comes down to 0s and 1s?

This is how data is stored on computers

These are really voltages (low for 0 and high for 1)

Storage Units
– Basic Storage Unit: Binary Digit (Bit)
– Basic Grouping: 8-bits (Byte)
– Natural Unit of Data: Word (Varies by computer – example 64-bit
computer)
Data Representation

In computers, data is represented in many different ways:
– Numbers
– Text
– Audio
– Images
– Video

All of these are made up of bits
Analog vs. Digital Data

Analog is continuous data, for example the range of real
number values from 0 to 1

Digital is a discrete (countable) representation
– The data has gaps between elements

Digital computers are by far the most commonly used today

Although analog computers fell out of favor, in recent years,
they are making (a bit of) a comeback
Data Representation: Numbers

We have seen how non-negative integers are
represented using bits (binary numbers)

What about negative integers?

What about real numbers?
Data Representation: Numbers

If integers are infinite, how do we fit them into a
finite amount memory?
– There are limitations to the maximum value
– Trade-offs

Accuracy

Maximum value
Data Representation: Numbers
Let’s look at Positive Numbers in 8-bits of Memory

When Integers are stored as only Positive or 0, we call them:


Unsigned Integers
Negative Numbers

We have looked at positive integers in binary

What about negative values?
– These examples will use 8-bits for the numbers but
the concepts translate to any size of word
Negative Integers in Binary

Binary numbers use a single bit to state whether the number
is positive or negative

This bit is called the “sign bit” and is found in the leftmost
place of the number
The sign bit goes here -> __ 0 1 1 0 1 1 1

If the sign bit is 0, the number is positive

If the sign bit is 1, the number is negative

You need to explicitly state if an integer is signed or unsigned
Negative Integers in Binary

We will look at three different ways negative
integers are represented in binary
1) Signed Magnitude
2) One’s Complement
3) Two’s Complement
Signed Magnitude

Also called “Sign Magnitude” and “Sign and Magnitude”

Just use the most significant bit (left-most) as the sign bit,
and the remaining 7 bits for the conversion:
011000112 = 9910
111000112 = -9910

If we were using unsigned integers then all 8 bits are used
for the conversion:
111000112 = 22710
Signed Magnitude
Example
If this number was unsigned it would be 13310
10000101

If it’s signed? Well, now the left-most 1 represents the sign.


Skipping the left-most 1, we get 0000101, which is 5.
So, in sign magnitude notation, 10000101 would be -5.
Signed Magnitude

So, what would this number be?
00000000

Yes, it is of course, zero. And what would this number be?
10000000

This is negative zero. Is zero a positive or negative integer?

Having +0 and -0 makes math difficult, and makes
computers less efficient

Let’s look at another method
One’s Complement

To get the negative value of a number (i.e. multiply the value by -1), we
flip all the bits. All 0 bits become 1 bits and all 1 bits become 0 bits.
011000112 = 9910
100111002 = -9910

Complements have interesting mathematical properties (which are
beyond the scope of this discussion)

However, the addition is still not completely easy, and there’s still the
problem of two 0s:
000000002 = +010
111111112 = -010
Two’s Complement

Two’s Complement is what is used on virtually all computers today.

To get the negative value of a number there are two steps:
Step 1: Flip all the bits (just like with One’s Complement)
011000112 = 9910
100111002
Step 2: Add 1 to the result
100111002
+1
10011101 = -9910
Two’s Complement

Two’s Complement gets rid of the two versions of zero. Let’s try to get -0
Flip the Bits: 00000000 → 11111111
Add 1 to the result:
11111111
+1
100000000

Note that there are 9 bits in the result. The leftmost bit is a carry bit that can’t
be held within 8 bits. So, it is discarded, leaving the result as:
000000002 = 010
So, only one representation of 0
Two’s Complement - Math

But, does math work properly?

It turns out yes. Two’s Complement has a
straightforward way to do math

Suppose we want to subtract 1 from 10
10 - 1

Instead of subtracting 1, we add its complement
10 + (-1)
Two’s Complement - Math
000010102 (1010)

+ 111111112 (-1 in Two’s Complement)

1000010012 (result in Two’s Complement)


Note that we have an overflow bit. Truncating it
gives us 00001001 which is 9

In Two’s Complement, all you need to do is add
Two’s Complement – Find Positive

We know how to get the complement (negative value) of a positive
integer. How do we get the positive value of a negative integer?

No need to reverse the process! Just follow the same process:
Step 1: Flip all the bits (just like with One’s Complement)
100111012 = -9910
011000102
Step 2: Add 1 to the result
011000102
+1
011000112 = 9910
Practice
● What is the negative representation of 4510 (001011012) in:
– Signed Magnitude:
101011012
– One’s Complement
110100102
– Two’s Complement
110100112
Practice

Give the binary (using 8 bits) representation of
the number -118 in
– Signed Magnitude
– One’s Complement
– Twos Complement
Practice Answers

Give the binary (using 8 bits) representation of the
number -118 in

118 in binary is 01110110
– Signed Magnitude

Just flip the sign bit --> 11110110
– One’s Complement

Flip all the bits --> 10001001
– Twos Complement

Flip all the bits and add 1 --> 10001001 + 1 = 10001010
More Practice
Show the steps to do the following operation using two’s
complement:
74 – 83 = ?
First, find the binary values of the numbers 74 and 83:
7410 = 010010102
8310 = 010100112
Next, take the two’s complement of 83:
-83 = 10101101
74 – 83 = ?
Now, add -83 to 74:
01001010 = 74
+ 10101101 = -83 in two’s complement
11110111 = a negative value (sign bit is 1)
The result is negative, but what is its value?
Find the two’s complement of 11110111
00001000 Flip the bits
+ 1 Add 1
00001001 = 9
So, the result of 74 – 83 is -9
Two’s Complement - Overflow

When adding/subtracting using two’s
complement, if one value is the opposite sign of
the other, overflow will never occur.

When overflow does occur, the carry bit is
thrown away.

What happens to results if the carry bit is
thrown away?
Two’s Complement - Overflow
What is the result of 118 + 31 using 8-bit two’s
complement?
11810 = 011101102
+ 3110 = + 000111112
14910 = 100101012 <- A negative value

Programmers must be mindful of the size of the integer


values that they are working with to avoid integer overflow
Floating Point Numbers
How can we represent real numbers based on what we know about integers?

We just write things in normal base 10 as always.

internal binary representation: like scientific notation.

sign exponent fraction


S: sign bit E: bias-adjusted exponent M: adjusted fractional value
value = (-1)S * 2E * M
Java float: 32-bit representation. (1 sign, 8 exp, 23 frac)
Java double: 64-bit representation. (1 sign, 11 exp, 52 frac)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy