Binary and all that…
Download
Report
Transcript Binary and all that…
Binary and all that…
CP4 Revision
CP4
The Teacher
The Binary Number System
• Binary is a Base 2 Number System
• Only uses digits 0 and 1
• Each digit is called a BIT – short for Binary
digIT
CP4
The Teacher
Hexadecimal
•
•
•
•
Shorthand way of writing binary numbers
Base 16 number system
Uses digits 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
Each hex digit represents a 4-bit binary
number.
• 6 = 0110
• C = 1100
CP4
The Teacher
Hex Exercise
1. Convert 011010100011111016 into hex
2. Convert B216 into binary
CP4
The Teacher
Characters
•
•
•
•
•
•
Each character has a unique binary code.
Most commonly used system is ASCII
A = 01000001
B = 01000010
C = 01000011
etc
CP4
The Teacher
Positive Integers
• Calculate using the headings…
128
64
32
16
8
4
2
1
Example : Binary representation of 7410
128
64
32
16
8
4
2
1
0 1 0 0 1 0 1 0
CP4
The Teacher
Negative Integers
• Two methods :
– Sign and Magnitude
– Two’s Complement
CP4
The Teacher
Sign and Magnitude
• First bit is a Sign bit
(0 for Positive; 1 for
Negative).
• Remaining bits are
the Size of the
integer.
Example : -21
Sign
64
32
16
8
4
2
1
1 0 0 1 0 1 0 1
CP4
The Teacher
Two’s Complement
• Three steps…
– Treat as positive
– Change all bits
– Add 1
Step 1 :
128
64
32
16
8
4
2
1
0 0 0 1 0 1 1 0
Step 2:
128
64
32
16
8
4
2
1
1 1 1 0 1 0 0 1
Example : -22
Step 3:
+1
1 1 1 0 1 0 1 0
CP4
The Teacher
Real Numbers
• Two methods –
– Fixed Point
– Floating Point
CP4
The Teacher
Fixed Point
• The position of the binary point remains fixed.
• Eg.: 4 points for the Integer part and 4 bits for
the fraction…
S
4
2
1
0 1 0 1
1/2
.
1/4
1/8
1/16
0 1 0 0
= 5.25
CP4
The Teacher
Floating Point
• Floating Point Format –
Mantissa x 2 Exponent
Mantissa is a Signed Fraction (Fixed Point)
Exponent is a Signed Integer
CP4
The Teacher
Convert a real to Floating Point Form
• Example : 9.75
• Convert into fixed point binary number…
Sign
8
4
2
1
.
.5
.25
0
1
0
0
1
.
1
1
• Add 0 bits on the right until the correct number of bits for
the mantissa : 01001.1100000
(assume 12 bit mantissa, 4 bit exponent for this)
• Move binary point to left until it is after the sign bit –
count how many moves - because that is the exponent
• 0.10011100000 and the exponent is 4
• Final answer : 010011100000 0100
CP4
The Teacher
Binary Exercises
1.
Using 8-bit Two’s Complement system…how would
these numbers be represented?
1.
2.
4310
-4310
2.
Using Sign and Magnitude system, how would the
above numbers be represented?
3.
Convert your answers to [1] into hexadecimal.
CP4
The Teacher
Binary Exercises
•
[CP4 2006]
•
(i) Convert the 12 bit binary number 101111010111 to hexadecimal. [1]
(ii) Why is hexadecimal often used as an alternative to binary? [1]
•
[CP4 2005]
• In a certain computer, two's complement is used to represent
negative integers, using 8 bits.
• (i) Show how the number -710 is represented. [1]
• (ii) Showing your working, demonstrate that 1310 is the result of the
binary addition of -710 to +2010. [2]
• (b)In another computer, a sign/magnitude approach is used to
represent integers using 8 bits. Explain what is meant by the term
sign/magnitude, giving a clearly labelled example [2]
• (c)In another computer, the character '1' is stored as 00110001. The
character '2' is stored as the next higher binary code (00110010)
and so on. How will the character '5' be stored? [1]
CP4
The Teacher