Download (number systems), 10

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
BINARY: WHAT IS IT???
Electronics: 0,1 is easy numbering system
Data is stored as 32 or 64 bits of 0s and 1s:
Registers, Memory, Disk, CDs, DVDs, ROM, Data
Communications
 Bit = 1 binary digit: 1 or 0
 Boolean: True = 1, False = 0
 ASCII: 8 bits
 Number = 8 or 16 or 32 or 64 (or 128) bits
OCTAL – HEXADECIMAL: WHAT IS IT?
1s & 0s are tedious:
 1010 0101 1111 0011 1100 0000 1001 0110 1110 0001
 Hexadecimal: 0x a 5 f 3 c 0 9 6 e 1
 Octal: 5 1 3 7 1 7 0 0 4 5 5 6 0 4
Octal and Hexadecimal are compatible; powers of 2; easy representations
of binary
Numbering Systems:
 Base 2: Binary
 Base 8: Octal
 Base 10: Decimal
 Base 16: Hexadecimal
WHY IS IT USEFUL?
Uses include:
UNIX Permissions:
740 = Self: Read, Write, Execute; Group: Read; Others: Null
Data Communications: Meaning of:
Transmission: 7E 24 35 37 3a b3 fd 00 23 33 fa 4c 5d da 98 62 31 11
a2 7f 7e
Routing tables: 4a.62.33.c0/26
Data: Setting flags, clearing bits, efficient storage
Engineering: Designing and working with memory
Debugging: Core dumps, reverse engineering
LOOK AT BASE 10 - DECIMAL
DECIMAL SYSTEM
0 1 2 3 4 5 6 7 8 9
O B S E R V AT I O N S
Modulo 10
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
10 = 101
100 = 102
1000 = 103
1234 = (1x103) + (2x102) +
(3x101) + 4
LOOK AT BASE 2 - BINARY
BINARY SYSTEM
O B S E R V AT I O N S
0
1
Modulo 2
10 11
10 = 21 = 2
100 = 22 = 4
1000=23 = 8
10112 = (1x23) + (0x22) +
(1x21) + 1 = 1110
100 101 110 111
1000 1001 1010 1011
1100 1101 1110 1111
10000 10001 10010 10011
10100 10101 10110 10111
LOOK AT BASE 8 - OCTAL
O C TA L SY S T E M
0 1 234567
O B S E R V AT I O N S
Modulo 8
10 11 12 13 14 15 16 17
20 21 22 23 24 25 26 27
30 31 32 33 34 35 36 37
…
70 71 72 73 74 75 76 77
100 101 102 103 104 105 106
10 = 81 = 8
100 = 82 = 64
1000=83 = 512
12348 = (1x83) + (2x82) +
(3x81) + 4 = 66810
LOOK AT BASE 16 - HEXADECIMAL
HEXADECIMAL SYSTEM
O B S E R V AT I O N S
0 1 234567 8 9 a b c d
e f
Modulo 16
10 11 12 13 14 15 16 17 18 19 1a
1b 1c 1d 1e 1f
20 21 22 23 24 25 26 27 28 29 2a
2b 2c 2d 2e 2f
…
f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc
fd fe ff
100 101 102 103 104 105 106 107
108 109 10a 10b 10c 10d 10e 10f
10 = 161 = 16
100 = 162 = 256
1000=163 = 4096
1a3f16 = (1x163) + (ax162) +
(3x161) + f = 671910
WORKING WITH BASE 2: BINARY
Each binary digit is a double of the digit to its right:
1
1
1
1
1
1
1
1
1
256
128
64
32
16
8
4
2
1
Binary can be noted as: B1011 or as 1011 2
So converting binary to decimal: (~=NOT)
 B1011 = 8 + ~4 + 2 + 1 = 11
 B101010 = 32 + ~16 + 8 + ~4 + 2 + ~1 = 32 + 8 + 2 = 42
 What is B01010101?
ADDING WITH BINARY
E Q U AT I O N S W I T H
CARRIES
B1111 1111
O B V I O U S E Q U AT I O N S
1 + 1 = 10
+B 1 0 0 1 1 1 0 0
100 + 11 = 111
B11 0 0 1 1 0 1 1
1001 + 100 = 1101
ADDING WITH BINARY
E Q U AT I O N S W I T H
CARRIES
O B V I O U S E Q U AT I O N S
carry:1 1 1 1 1
B1111 1111
1 + 1 = 10
+B 1 0 0 1 1 1 0 0
100 + 11 = 111
B11 0 0 1 1 0 1 1
1001 + 100 = 1101
Sum: 3 2 2 3 3 2 1 1
NOT OPERATION ~
EXAMPLES:
NOT
The opposite of 0
is 1
Not_flag()
If flag = false
flag = true
else flag = false
N OT T R U T H TA B L E
NOT ~
Input
0
Output
1
1
0
~0 = 1
~1011=0100
~1 = 0
~0101=1010
AND OPERATION - &
EXAMPLES:
AND
If (you are >=18 AND
you are registered to
vote) then
You can vote
If (a==b && a==c)
print(“a = b = c”);
A N D T R U T H TA B L E
AND &
0
1
0
0
0
1
0
1
1&1=1
1&0=0
0&0=0
0&1=0
If either input is false, result is false
OR OPERATION - |
EXAMPLES:
OR
If (you are born in US
OR you pass Citizen
exam) then
You are US Citizen
If (a==b | a==c)
print(“a is b or c”);
O R T R U T H TA B L E
OR |
0
1
0
0
1
1
1
1
1|1=1
1|0=1
0|0=0
0|1=1
If either input is true, result is true
XOR OPERATION - ⊕
EXAMPLES:
XOR
If (a!=b )
print(“no match = 1”);
else // match
print(“match = 0”);
Uses: Encryption, etc.
XO R T R U T H TA B L E
OR ⊕
0
1
0
0
1
1
1
0
1⊕1=0
1⊕0=1
0⊕0=0
0⊕1=1
ANDING/ORING LONGER NUMBERS
A N D O P E R AT I O N
O R O P E R AT I O N
1011 0111
1011 0111
& 0111 0000
| 0111 0000
0011 0000
1111 0111
1100 0011
0000 0000
&1111 0000
| 0101 0000
WHY ANDS AND ORS?
AND
OR
Useful for turning off
bits
Useful for turning on
bits
Clear a field
Set a flag
Clear a flag
Set a value into a field
Sign
1 bit
Exponent
8 bits
Fraction
23 bits
CONVERTING BASE 16 -> BASE 10
METHOD 1: CONVERT TO
B IN A RY, T HE N D E C IM A L:
0x1af = 0001 1010 1111
= 20 + 21 + 22 + 23 + 25 + 27 + 28
METHOD 2: USE
DIVISION REMAINDERS:
Convert from base 10 to base N
(Example base 2):
= 1 + 2 + 4 + 8 + 32 + 128 + 256
= 43110
0x456 = 0100 0101 0110
= 210+26+24+22+21
= 1024 + 64 + 16 + 4 + 2
= 111010
Number / 2 ->remainder=>digit0
-> quotient / 2 -> remainder =>digit1
-> quotient / 2 -> remainder =>digit2
CONVERTING BASE 16 -> BASE 10
EXAMPLE METHOD 2:
Example 1: Convert 3610 into binary:
Quotient/2 ->Remainder
36/2
->0
18/2
->0
9/2
->1
4/2
->0
2/2
->0
1/2
->1
3610 =
1001002
METHOD 2: USE
DIVISION REMAINDERS:
Convert from base 10 to base N
Number / n ->remainder=>digit0
-> quotient / n -> remainder =>digit1
-> quotient / n -> remainder =>digit2
Example 2: Convert 3610 into base 16:
36/16 ->4
2/16
->2
3610 =
2 416
FORMING A NEGATIVE NUMBER
1'S COMPLEMENT
0=0000 -0=1111?
2'S COMPLEMENT
0=0000
1=0001 -1=1110
1=0001 -1=1111
2=0010 -2=1101
2=0010 -2=1110
3=0011 -3=1100
3=0011 -3=1101
4=0100 -4=1011
4=0100 -4=1100
Total: +7 -7
Total: +7 -8
0001
0000
0010
1111
0011
1110
0100
2s
Comp
1101
0101
1100
0110
1011
0111
1010
1000
1001
SIGNED & UNSIGNED NUMBERS
Binary
00000000
00000001
00000010
01111110
01111111
10000000
10000001
10000010
11111110
11111111
Signed
0
1
2
+126
+127
-128
-127
-126
-2
-1
Unsigned
0
1
2
+126
+127
+128
+129
+130
+254
+255
Assumes 1-byte storage
SIGNED & UNSIGNED INTEGERS
Signed integers
•
Use when numbers may be negative
•
To create negative numbers, the high-order (top) bit is the signed bit.
0=Positive Number
1=Negative Number
Unsigned integers
•
Use when all numbers are POSITIVE.
•
No overflow to negative numbers are possible then
Accuracy - Security:
•
Do you want positive only – or positive & negative?
•
Signed: Incrementing goes from large positive to large negative.
CONVERTING TO DECIMAL: POWERS OF TWO
The sign bit (bit 7) indicates both sign and value:
•
If top N bit is ‘0’, sign & all values are positive: top set value: 2N
•
If top N bit is ‘1’, sign is negative: -2N
•
Remaining bits are calculated as positive values:
10101010 = -27 + 25 + 23 + 21 = -128 + 32 + 8 + 2 = -86
01010101 = 26 + 24 + 22 + 20 = 64 + 16 + 4 + 1 = 85
CHANGING SIGNS: TWO’S COMPLIMENT
A positive number may be made negative and vice versa using this technique
Method: Take the inverse of the original number and add 1.
Original:
01010101 = 85
10101011 = -85
invert:
10101010
01010100
add 1:
+1
+1
sum:
10101011 = -85
01010101 = 85
CHANGING SIGNS: TWO’S COMPLIMENT
Why does this work?
Identity + Inverse = -1
Number: 10101010
Inverse:
01010101
Sum:
11111111 = -1
If
x + x’ = -1
Then
x + (x’ + 1) = 0
And
x’ + 1 = -x
SHIFTING BITS
SHIFT LEFT
SHIFT RIGHT
E.g.: 0xa9 =1010 1001
E.g.: 0xa9 = 1010 1001
Shift left 1: 0101 0010
Shift right 1: 0101 0100
Shift left 1:
Shift right 1:
Shift left 2:
Shift right 2:
New hexadecimal value:
New hexadecimal value:
SHIFTING IS USEFUL
Move bits into position
Extract a field
Example:
1.0111011 x 2101
Sign = 0 (positive)
Exponent = B101 = 5
Shift and Or all fields together to get float value
Sign
1 bit
Exponent
8 bits
Fraction
23 bits
Computers operate in
binary
It is important to
‘speak’ binary and
hexadecimal
Base 2 & 16 will be
used in a number of
other courses
Related documents