0% found this document useful (0 votes)
26 views21 pages

3 Data Integer Representation V1

1) Integers in computers are represented using binary numbers in formats like unsigned, sign-magnitude, one's complement, and two's complement. 2) Two's complement is the most common representation as it allows the same arithmetic operations for signed and unsigned numbers. 3) In two's complement, the most significant bit determines if a number is positive or negative, and flipping bits and adding one is used to convert between positive and negative numbers.

Uploaded by

Engy Ahmed
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)
26 views21 pages

3 Data Integer Representation V1

1) Integers in computers are represented using binary numbers in formats like unsigned, sign-magnitude, one's complement, and two's complement. 2) Two's complement is the most common representation as it allows the same arithmetic operations for signed and unsigned numbers. 3) In two's complement, the most significant bit determines if a number is positive or negative, and flipping bits and adding one is used to convert between positive and negative numbers.

Uploaded by

Engy Ahmed
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/ 21

Integer Representation

CSE 311 – Computer Organization


Assoc. Prof. Ahmed Fares
Today
• How integers (signed and unsigned) are represented in modern computer
systems

6
Representing unsigned integers
• Given n bits to store an integer, we can represent 2n different values
• If we just care about non-negative (aka unsigned) integers, we can easily
store the values
0, 1, 2, …, 2n-1
• E.g., for 4 bits
• 0x2 = 2
• 0xB = 11
• 0xF = 15 = 24-1
• Binary number

7
Representing negative integers
• We have seen how to represent unsigned integers (i.e., non-negative
integers) as unsigned binary numbers
• Every number between 0 and 2w-1 has a unique encoding as a w-bit value
• Addition works as we expect it to
• How do we represent negative integers?
• Three common encodings:
• Sign and magnitude
• Ones’ complement
• Two’s complement

8
Sign and magnitude
• Use one bit to represent sign, remaining bits represent magnitude
• With n bits, have n-1 bits for magnitude
• E.g., with 4 bits, can represent integers
-7, -6, …, -1, 0, 1, …, 6, 7
• MSB (most significant bit) represents the sign
• 0 is positive
• 1 is negative
1011 represents -3

sign: - magnitude: 3

9
Properties of sign and magnitude
• Advantages:
• Straight-forward and intuitive
• Issues:
• Arithmetic operations need different implementations for signed and unsigned
numbers
• E.g., addition, using 4 bits
• unsigned: 0001 + 1001 = 1 + 9 = 10 = 1010
• sign and magnitude: 0001 + 1001 = 1 + -1 = 0, which is not 1010
• Two different representations of zero!
• E.g., using 4 bits, 1000 and 0000 both represent zero!

10
Binary Ones’

Ones’ complement 0000


Complement

0
0001 1
• Negation is performed by performing a bitwise not (~) on
the number 0010 2
• In other words, flip all the bits in the number 0011 3
• For example, using 4 bit numbers: 0100 4
• 6= 0110
• -6 = 1001 0101 5

• Like sign and magnitude, first bit indicates whether number 0110 6
is negative 0111 7
• If the msb (most significant bit) is 0, treat it like an unsigned binary 1000 -7
number
1001 -6
• If the msb is 1, the number is negative, flip all the bits to see its
magnitude 1010 -5
• Using n bits, can represent numbers 2n-1 values 1011 -4
• E.g., using 4 bits, can represent integers 1100 -3
-7, -6, …, -1, 0, 1, …, 6, 7 1101 -2
1110 -1
1111 -0
11
Properties of ones’ complement
• We still have two different representations of zero
• E.g., using 4 bits, 1111 and 0000 both represent zero
• Same implementation of arithmetic operations for signed and unsigned!
• E.g., addition using 4 bits
• unsigned: 0001 + 1001 = 1 + 9 = 10 = 1010
• ones’ complement: 0001 + 1001 = 1 + -6 = -5 = 1010

CMPU 224 -- Computer Organization 12


Two’s complement Binary Ones’ Two’s
Complement Complement

0000 0 0
• Take the ones’ complement of the number and 0001 1 1
add one 0010 2 2
• Flip all the bits and add one to the number 0011 3 3

• Example: Using 4-bit numbers take the two’s 0100 4 4


0101 5 5
complement of 3 0110 6 6
0111 7 7
1000 -7 -8
1001 -6 -7

• Like sign and magnitude and ones’ 1010 -5 -6


1011 -4 -5
complement, first bit indicates whether
1100 -3 -4
number is negative 1101 -2 -3
1110 -1 -2
1111 -0 -1
13
Properties of two’s complement
• Same implementation of arithmetic operations for signed and unsigned
• E.g., addition, using 4 bits
• unsigned: 0001 + 1001 = 1 + 9 = 10 = 1010
• two’s complement: 0001 + 1001 = 1 + -7 = -6 = 1010
• Only one representation of zero!
• Simpler to implement operations
• Not symmetric around zero
• Can represent one more negative number than positive numbers
• Using n bits, can represent numbers 2n values
• E.g., using 4 bits, can represent integers
-8, -7, …, -1, 0, 1, …, 6, 7
• Most common representation of negative integers in computers
14
Converting to and from two’s complement
• To encode a negative number in two’s complement in n bits:
• Compute out the binary notation for the absolute value using all n bits
• Invert the bits and add 1
• E.g., to encode -5 using 8 bits
• 5 = 00000101 using all 8 bits
• Invert the bits: 11111010
• Add one: 11111010 + 1 = 11111011
• -5 encoded in two’s complement using 8 bits is 11111011
• To decode a two’s complement number:
• If the msb is 0 then number is positive
• If the msb is 1, then number is negative:
• invert bits and add 1
• Value gives you the magnitude of the negative number

15
Two’s Complement Interpretation
• You can interpret a two’s complement number as having a “negative
weighting” in the MSB
-23 = -8 22 = 4 21 = 2 20 = 1
-8 +4 +2 +1

• For any two’s complement representation


• The largest negative number will have a 1 in the msb and the rest of the bits 0
• The largest positive number will have a 0 in the msb and the rest of the bits 1
• -1 is represented by all ones

16
Quick two’s complement conversion
• One trick for taking the to two’s complement of a number quickly
• Flip all the bits, then starting from the lsb, flip all the 1s you see to 0s until you get
to a 0, flip that 0 to a 1 and stop
• Example: using 6-bit two’s complement numbers, convert 4 to -4
• Write out 4 in binary using 6 bits
• Flip all the bits
• Flip the rightmost 1’s and the first 0
• For negative two’s complement numbers, leading 1s are similar to leading
0s for positive numbers

17
Signed vs. unsigned in C
• int x; // declares x as a signed integer
• unsigned int x; // declares x as an unsigned integer
• Constants are signed by default
• 42 // would be stored as a two’s complement number
• 42u // would be stored as an unsigned binary number
• C allows conversion between unsigned and signed
• Most systems follow the rule that the underlying bit representation does not
change
int main(void) {
unsigned char ux = 0xff;
char x = ux;
printf("%d %d\n", ux, x);
}

18
Mapping Signed  Unsigned
Bits Signed Unsigned
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4
= 4
0101 5 5
0110 6 6
0111 7 7
1000 -8 8
1001 -7 9
1010 -6 10
1011 -5 +/- 16 11
1100 -4 12
1101 -3 13
1110 -2 14
1111 -1 15
15
Relation between Signed & Unsigned

Two’s Complement Unsigned


T2U
x T2B B2U ux
X

Maintain Same Bit Pattern

w–1 0
ux + + + ••• + + +
x - + + ••• + + +

Large negative weight


becomes
Large positive weight

16
Expanding the bit representation of a number
• How do you convert from a smaller integer data type to a
larger integer data type (e.g., a short to an int) while
retaining the same value?
• Should always be possible because the larger type will have a
wider range of numbers than the smaller type
• To convert an unsigned number to a larger data type:
• Add leading zeros to the representation
• Known as zero extension
• To convert a two’s complement number to a larger data
type:
• Perform sign extension
• Add copies of the msb (the sign bit) of the smaller
representation to the extra bits of the larger representation

19
Truncating Numbers
• When reducing the number of bits in a number, the smaller number may
not be able to correctly represent the larger number
• We reduce the number of bits representing a number by truncating the
uppermost (high-order) bits
• Reinterpret this truncated number
• For unsigned numbers, it is equivalent to performing a mod 2k on the
original value where k is the smaller bit width
• A similar property holds for signed numbers except that it then converts
the msb into a sign bit
• Which means it is possible that truncating a signed number changes its sign
• Rule of thumb: if the larger number is within the expressible range of the
smaller number, the conversion will yield a correct result

20
Expanding and Truncating Rules
• Expanding (e.g., short to int)
• Unsigned: zeros added
• Signed: sign extension
• Both yield expected result

• Truncating (e.g., int to short)


• Unsigned/signed: high order bits are truncated (drop)
• Result reinterpreted
• For small numbers this yields expected behavior
11111010 → -6 8-bit two’s complement
1010 → -6 4-bit two’s complement
• Truncating can result in a sign change

19
Values for Different Word Sizes

W
8 16 32 64
UMax 255 65,535 4,294,967,295 18,446,744,073,709,551,615
TMax 127 32,767 2,147,483,647 9,223,372,036,854,775,807
TMin -128 -32,768 -2,147,483,648 -9,223,372,036,854,775,808

• Observations
• |TMin | = TMax + 1
• Asymmetric range
• Umax = 2 * TMax + 1

20
What is going on with this cartoon?

They are counting sheep using a 16-bit two’s complement number!

21

You might also like