You are on page 1of 5

CS 117 Number representation

Spring 2004
Computers store numbers, text, .... (any data) in
binary
computers are just electrical circuits


binary numbers (1, 0) are indications of voltage levels
Binary representation of numerical


through these circuits (1 = “high”, 0 = “low”)
data Text, audio, video: later
Numbers: need to represent
April 14, 2004 integers


reals (decimal values)


negative and positive numbers


Integers Integers in base n
Base-10 integers: have a series of digits whose Have same notion of position and digits
value depends on the relative position of these digits: {0, 1, ... n-1}


digits
positions: powers of n


e.g. 543 = 5 100's, 4 10's, 3 1's
Example:


= 5*102 + 4*101 + 3*100





1342 in base 6 = 1*63 + 3*62 + 4*61 + 2*60
e.g. 13245 = 1*104 + 3*103 + 2*102 + 4*101 + 5*100


10101 in base 2 (binary) = 1*24 + 0*23 + 1*22 + 0*21




Positions are powers of 10, digits are {0, 1, ..., 9} + 1*20


Converting from base n to decimal Converting from decimal to base n
Multiply digits by positions in the base n number, Divide the decimal number repeatedly by the base



as on the previous slide idea: find the largest power of n that fits into that


Examples: decimal number


1342 in base 6 = 1*63 + 3*62 + 4*61 + 2*60 = 350 in at each stage, the remainder becomes the leftmost




base 10 digit

10101 in base 2 (binary) = 1*24 + 0*23 + 1*22 + 0*21 carry the quotient to the next stage




+ 1*20 = 21 in base 10 repeat until the quotient is zero


00100101 in binary = 1*25 + 1*22 + 1 = 37 in base 10


Converting from decimal to base n: Example cont.


examples
982 from decimal to binary:


543 to base 5:


982/2 = 491 rem 0


543/5 = 108 rem 3 491/2 = 245 rem 1




245/2 = 122 rem 1


108/5 = 21 rem 3




122/2 = 61 rem 0


21/5 = 4 rem 1


61/2 = 30 rem 1

4/5 = 0 rem 4 30/2 = 15 rem 0


= 4133 15/2 = 7 rem 1





7/2 = 3 rem 1
982 to binary = ?



3/2 = 1 rem 1


1/2 = 0 rem 1


= 1111010110

Representing negative numbers in Another way to represent negative
binary numbers: two's complement
Allows us to represent negative numbers in


One approach: signed-magnitude


binary, without the “two zeros” problem


for an 8-bit number, the first bit is the sign


(0=positive, 1=negative), and the other 7 bits are the All positive numbers start with a 0, as before


value Negative numbers:


can represent all numbers from -127 to 127


Invert all the bits (change 0's to 1's and 1's to 0's)


examples:


Add 1 to this number


1 = 00000001, -1 = 10000001


With 8 bits, can represent all numbers from -128


65 = 01000001, -65 = 11000001


0 = 00000000, -0 = 10000000 --> problem! to 127




one extra than with signed-magnitude


More two's complement examples Scientific notation
-7: Expressing very large or very small numbers can



7 is 00000111 be tedious


invert bits: 11111000 e.g., 2147483647, 0.245689734921




add 1: 11111001 Scientific notation allows us to express large or





-127: small numbers more compactly




127 is 01111111 show us the “order of magnitude” of the number,





rather than all of the digits


invert bits: 10000000


Format: mantissa x 10exponent




add 1: 10000001


-1: 00000001 -> 11111110 -> 11111111



Examples Representing real numbers in binary
2147483647 = 2.147 x 109 Use scientific notation: a x 2N


0.245689734921 = 2.457 x 10-1 a and N are both binary

0.0000000000000567 = 5.67 x 10-14 Format: (IEEE 754 32-bit standard)

1 bit for the sign

8 bits for the exponent (N)

23 bits for the mantissa (a)

Converting decimal (fractional) Example


number to binary Convert 0.25 to binary:


Multiply fractional part by 2 0.25*2 = 0.5

0.5 * 2 = 1.0
Integer part of the answer becomes the next

rightmost digit in the number = 0.01 * 20

Convert -10.125 to binary:



Carry the fractional part to the next step

step 1: convert 10 to binary --> 1010


Repeat until the fractional result of multiplication

is 0, or until the desired precision is reached step 2: convert .125 to binary:


0.125*2 = 0.250; 0.250*2 = 0.5; 0.5*2 = 1.0


= 0.001

step 3: put them together


= -1010.001 * 20 = -0.1010001 * 24

Previous examples, in IEEE 794
format
0.25 = 0.01 * 20 =

00000000001000000000000000000000
-10.125 = -0.1010001 * 24 =

10000010010100010000000000000000

You might also like