Binary Representation for Numbers Assume 4bit numbers 5 as an integer 0101 5 as an integer How 50 as a real number How What about 55 Sign Bit R eserve the mostsignificant bit to indicate sign ID: 459832
Download Presentation The PPT/PDF document "Binary Representation" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Binary RepresentationSlide2
Binary
Representation for Numbers
Assume 4-bit numbers
5 as an integer
0101
-5 as an integer
How?
5.0
as a
real number
How?
What about 5.5?Slide3
Sign Bit
R
eserve the most-significant bit to indicate sign
Consider integers in 4 bits
Most-significant
bit is sign: 0 is positive, 1 is negative
The 3 remaining bits is magnitude
0
010 = 2
1
010 = -2
How many possible combinations for 4 bits?
How many unique integers using this scheme?Slide4
Two’s Complement
Advantages
# of combinations of bits = # of unique integers
Addition is “natural”
Convert to two’s complement (and vice versa)
invert the bits
a
dd one
i
gnore the extra carry bit if present
Consider 4-bit numbers
0010
[2]
-> 1101 -> 1110
[-2]Slide5
Addition
0010
[2]
+
1110
[-2]
0000
[ignoring
the final carry—extra bit]
0011
[3]
+ 1110
[-2]
0001
[1]
1110
[-2]
+ 1101
[-3]
1011
[-5]Slide6
Range of
T
wo’s Complement
4-bit numbers
Largest positive:
0111
(binary) => 7 (decimal)
Smallest negative: 1000 (binary) => -8 (decimal)
# of unique integers = # of bit combinations = 16
n
bits
?Slide7
Binary Real Numbers
5.5
101.1
5.25
101.01
5.125
101.001
5.75
101.11
…
2
3
2
2
2
1
2
0
.
2
-1
…Slide8
8 bits only
5.5
101.1 -> 000101 10
5.25
101.01 -> 000101 01
5.125
101.001 -> ??
With only 2 places after the point, the precision is .25
What if the point is allowed to move around?
2
5
2
4
2
3
2
2
2
1
2
0
2
-1
2
-2Slide9
Floating-point Numbers
Decimal
54.3
5.43 x 10
1
[scientific notation]
Binary
101.001
10.1001 x 2
1
[more correctly: 10.1001 x 10
1
]
1.01001 x 2
2
[more correctly: 1.01001 x 10
10
]
What can we say about the most significant bit?Slide10
Floating-point Numbers
General form:
sign
1.
mantissa
x 2
exponent
the most significant digit is right before the dot
Always 1 [no need to represent it
]
Exponent in Two’s
complement
1.01001
x 2
2
Sign
:
0 (positive)
Mantissa
:
0100
Exponent: 010 (decimal 2)Slide11
Java Floating-point Numbers
Sign:
1 bit [0 is positive]
Mantissa:
23 bits in
float
52 bits in
double
Exponent:
8 bits in
float
11 bits in
double
sign
exponent
mantissaSlide12
Imprecision in Floating-Point Numbers
Floating-point numbers often are only approximations since they are stored with a finite number of bits.
Hence
1.0/3.0
is slightly less than 1/3.
1.0/3.0 + 1.0/3.0 + 1.0/3.0
could be less than 1.
www.cs.fit.edu/~
pkc/classes/iComputing/FloatEquality.javaSlide13
Abstraction Levels
Binary
Data
Numbers (unsigned, signed [Two’s complement], floating point)
Text (ASCII, Unicode)
HTML
Color
Image (JPEG)
Video (MPEG)
Sound (MP3)
Instructions
Machine language (CPU-dependent)
Text (ASCII)
Assembly language (CPU-dependent)High-level language (CPU -independent: Java, C++, FORTRAN)