2e3 numbersystems week1 lecture2
TRANSCRIPT
-
8/21/2019 2E3 NumberSystems Week1 Lecture2
1/17
2E3: Computer Engineering IINumber Systems
Dr. Ivana Dusparichttp://www.scss.tcd.ie/Ivana.Dusparic/2E3/
Introduction to Number Systems
Why different number systems?
Why do we need to know them?
Binary, Octal, Hexadecimal – signed vs. unsigned
Conversion between number systems – Binary to/from decimal
– Binary to/from hexadecimal – Hexadecimal to/from decimal
C++ notation
Binary Operations – AND, OR, XOR, NOT
– Bitwise shift left/right
-
8/21/2019 2E3 NumberSystems Week1 Lecture2
2/17
Introduction to Number Systems
Humans use decimal number system
– 10 fingers
Computers – binary – 2 physical states– 0V and 5V– charge / NO charge on transistor gate
How to represent decimal digits using only 2states?– 3 or 4 bits depending on the digit – 4th bit often wasted– Use octal (3 bits) or hexadecimal (4 bits)
Binary
Unsigned vs. signed binary integers
– Unsigned – positive integers only
Base 2 notation:
– E.g. 3710 = 1001012
We normally deal in decimal = base 10
– 3710 = 3×101 + 7×100
– 40310 = 4×102 + 0×101 + 3×100
-
8/21/2019 2E3 NumberSystems Week1 Lecture2
3/17
Unsigned Binary Integers
100101
1’s2’s4’s8’s16’s32’s
Can do the same for any base, say base 2
– 1001012 = 1×25 + 0×24 + 0×23 + 1×22 + 0×21 + 1×20
– 1001012 = 3210 + 010 + 010 + 410 + 010 + 110
– 1001012 = 3710
Unsigned Binary Integers
Always remember what base you are operating in
– “Value” of a number is meaningless otherwise
Convert decimal to binary:
– Keep dividing by the base and read the remainders
9
4
2
1
0
182
2
2
2
2
372 1
0
1
0
0
1
Read frombottom 1001012
-
8/21/2019 2E3 NumberSystems Week1 Lecture2
4/17
Unsigned Binary Integers
pictorial view of a 4 bitunsigned binary integer
4 bit unsigned binaryinteger range:
00002 .. 111120 .. 15
n bit unsigned binaryinteger range:
0.. 2n-1
Unsigned Binary Integers - Exercise
Practise converting unsigned binary integers todecimal: – 01012 – 111001102
Practise converting from decimal to unsigned binaryintegers:
– 3410 – 12310 – 1010
Do not use calculator for conversion, but use it todouble-check the correctness of your result!
= 510
= 23010
= 1000102
= 11110112
= 10102
-
8/21/2019 2E3 NumberSystems Week1 Lecture2
5/17
Signed Binary Integers
Positive and negative integers
2's complement notationThe 2's complement of the
number behaves like the negativeof the original number
Convert -5 to a 2's complement
binary number by inverting bitsand adding 1
5 01012
invert bits 10102
add 1 00012
-5 10112
invert bits 01002
add 1 01012
5 01012
Signed Binary Integers
Same effect achieved by subtracting
from 0
4 bit signed binary integer positive range: 00002 .. 01112 0 .. 7 negative range: 11112 .. 10002 -1 .. -8
n bit signed binary integer range: -2n-1 .. 2n-1 – 1 Most significant bit indicates integer negative - sign
bit Note asymmetrical range – only one zero
zero 00002
subtract 5 01012
-5 10112
-
8/21/2019 2E3 NumberSystems Week1 Lecture2
6/17
Signed Binary Integers
pictorial view of a 4 bitsigned binary integer
if unsigned, inner ring
would have values 0 ..15
value depends on
how binary bits areinterpreted
Signed Binary Integers - Exercise
Practise converting 8-bit signed binary integers todecimal: – 000001012 – 111111012
Practise converting from decimal to signed 8-bitbinary integers: – -2810 – -12310 – -1010
= 510
= -310
= 111001002= 100001012= 111101102
To help you double-check your result
http://www.rsu.edu/faculty/PMacpherson/Programs/twos.html
-
8/21/2019 2E3 NumberSystems Week1 Lecture2
7/17
Hexadecimal
base 16
Same principles apply: – 15616 = 1×16
2 + 5×161 + 6×160
– 15616 = 25610 + 8010 + 610 – 15616 = 34210
But: – each binary digit has 2 possible values: {0, 1}
– each decimal digit has 10 possible values: {0, 1, … 8, 9}
– each hexadecimal digit must have 16 possible values:{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F }
Hexadecimal
012345678
9ABCDEF
012345678
9101112131415
Base 16 Base 10
=
BINARY HEX
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
1010 A
1011 B
1100 C
1101 D
1110 E
1111 F
-
8/21/2019 2E3 NumberSystems Week1 Lecture2
8/17
Hexadecimal
Hexadecimal to binary
Easier way to handle large binary numbers bygrouping 4 binary bits into a hexadecimal digit
Consider the following 16 bit unsigned binary integer
– 1111 1010 1100 11102 = FACE16 = 0xFACE
Hexadecimal
Hexadecimal to decimal
Now convert 0xFACE to decimal– F×163 + A×162 + C×162 + E×160
– 15×163 + 10×162 + 12×161 + 14×160
– 15×4096 + 10×256 + 12×16 + 14×1– 64,206
What decimal value is 0xFACE if interpreted as a 16bit signed binary integer??– 1111 1010 1100 1110 – leading bit =1, negative number
- flip bits and add 1 to get positive equivalent, and add minussign
-
8/21/2019 2E3 NumberSystems Week1 Lecture2
9/17
Hexadecimal
Convert decimal to hexadecimal
Convert -2008510 to hexadecimal (assume 16 bitsigned number
2008516
125516
7816
416
0
5
7
E
4
read from
bottom4E7516
20085 4E7516
invert bits B18A16
add 1 000116
-20085 B18B16
Hexadecimal Exercises
Convert to hexadecimal:
– 1010 – 3410 – 15010
Covert to decimal:
– 4516 – F16 – 1EA16
= A16
= 2216
= 9616
= 6910
= 1510
= 49016
-
8/21/2019 2E3 NumberSystems Week1 Lecture2
10/17
1
Hexadecimal Exercises
Convert from hex to binary:
– 12316 – FF16
Convert from binary to hex:
– 11012 – 01001102 – 1100000011111111111011102
= 1001000112
= 111111112
= D16
= 2616
= C0FFEE16
Hexadecimal
Hex is a convenient way to express pixelvalues:
Can ignore
11111111 11111111 00111111 00000111 Bit 0Bit 31
Red Green BlueAlpha
1111 1111 1111 1111 0011 1111 0000 0111F F F F 3 F 0 7
color = 0xFF3F07;
-
8/21/2019 2E3 NumberSystems Week1 Lecture2
11/17
1
Hexadecimal
Each colour channel (R, G, B) has 8 bits – Therefore 256 levels = {0, 255}
50% grey = all colours at 50%: – {50%, 50%, 50%}
– {12810, 12810, 12810} ← note rounding
– {100000002, 100000002, 100000002}
– {8016, 8016, 8016}
– int color = 0x808080;
Blue:– int color = 0x0000FF;
Purple:– int color = 0xFF00FF;
Number Systems in C++
Naming conventions
C/C++ syntax
4 bits nybble
8 bits byte
16 bits word
32 bits double word
64 bits quad word
C/C++ base decimal equivalent
1234 decimal 1234
0x1234 hexadecimal 4660
01234 octal (base 8) 2322
leading 0 indicates octal integer (beware!)
-
8/21/2019 2E3 NumberSystems Week1 Lecture2
12/17
1
Number Systems in C++
VC++ can handle 8, 16, 32 and 64 bit signed and unsigned arithmetic
int is CPU(and OS) dependent and maps to native word size of CPU
can determine a type's size using the sizeof() operator e.g. sizeof(int)
Type name # bytes range
char 1 –128 to 127
unsigned char 1 0 to 255
short 2 –32,768 to 32,767
unsigned short 2 0 to 65,535
int 4 –2,147,483,648 to 2,147,483,647
unsigned int 4 0 to 4,294,967,295
long long 8 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned long long 8 0 to 18,446,744,073,709,551,615
Number Systems in C++
To output in hex:std::cout
-
8/21/2019 2E3 NumberSystems Week1 Lecture2
13/17
1
Bit Manipulation
Required in any software talking directly to hardware
– E.g. graphics / audio etc.
Illustrate concepts in binary but generally work in hex
Binary operators:
– Take 2 parameters
Unary operators:
– Takes 1 parameter
– NOT operator is unary – Others = ++, --
a=~b;!~NOT
a=b>>c;n/a>>Shift Right
a=b
-
8/21/2019 2E3 NumberSystems Week1 Lecture2
14/17
1
Bitwise AND: &
a = b AND c
– a is TRUE if b and c areTRUE
– a is FALSE otherwise
E.g.
Useful for clearing bits:
0 0
0 1
0 1
0
1
AND
Truth table01101101
& 00111100
00101100
01101101& 11110001
01100001
Bit mask
if(a && b)
{
…
}
if(a & b)
{
…
}
≠≠≠≠
if a==true and b==true if (a&b) ≠ 0
Bitwise OR: |
a = b OR c
– a is TRUE if b or c areTRUE
– a is FALSE otherwise
E.g.
Useful for setting bits:
0 1
1 1
0 1
0
1
OR
Truth table
01101101
| 00111100
01111101
01101101
| 00001110
01101111
Bit mask
if(a || b)
{
…
}
if(a | b)
{
…
}
≠≠≠≠
if a==true or b==true if (a|b) ≠ 0
-
8/21/2019 2E3 NumberSystems Week1 Lecture2
15/17
1
Bitwise XOR/Exclusive OR: ^
a = b XOR c
– a is TRUE if b or c areTRUE but not both
– a is FALSE otherwise
E.g.
Useful for flipping bits:
0 1
1 0
0 1
0
1
XOR
Truth table01101101
^ 0011110001010001
01101101
^ 00001110
01100011
Bit mask
Bitwise NOT/ 1’s Complement: ~
a = NOT b
– a is TRUE if b is FALSE
– a is FALSE otherwise
E.g.
Useful for flipping all bits
of a value
1 0
0 1NOT
Truth table
~01101101 = 10010010
if(!a)
{
…
}
if(~a)
{
…
}
≠≠≠≠
if a≠true if (~a) ≠ 0
-
8/21/2019 2E3 NumberSystems Week1 Lecture2
16/17
1
Exercise
Try these (give answers in base of operands):
– 1111 00112 & 0011 11002 – 1010 01012 | 0101 01012 – ~(11001)2 assuming 8-bit
– 110012 ^ 1111112 8-bit also
– 810 | 1610 – 516 | 1516
– 23416 & F0F16 – ~(1FE)16 12-bit = 3 hex digits
Use calculator to double-check your results
= 0011 00002
= 1111 01012
= 1110 01102
= 0010 01102
= 2410
= 1516
= 20416= E0116
Size of Operands
Must be careful with bit operations:
– Always know the size of the operands
– E.g. 11112 & 1112 = ?
– What is ~100102 ?
– So must be clear what size of operand you’re workingon – is it 5 bits, a byte, or a word, or something else ?
11112& 1112
?1112
11112& 01112
01112
100102 = 0001 00102 = 0000 0000 0001 00102
011012 ≠ 1110 11012 ≠ 1111 1111 1110 11012
-
8/21/2019 2E3 NumberSystems Week1 Lecture2
17/17
Bit-wise Shift Left
Shift left operator: > 1 = 4310 = 430 / 101
B=2: 10112 >> 2 = 102(1110) >> 2 = (210) = 11 / 2
2