Post on 23-Apr-2020
transcript
Autumn 2019 CS101@CSE IIT Bombay
CS 101:Computer Programming and
Utilization
Puruwith
CS101 TAs and Staff
Course webpage: https://www.cse.iitb.ac.in/~cs101/
Lecture 9: Number representation
Autumn 2019 CS101@CSE IIT Bombay
Number representation and variables
02/08/19 2
Autumn 2019 CS101@CSE IIT Bombay
why numbers?• problem solving using computers is about
problem solving on numbers• examples
– salaries, temperature, length, distance, voltage …– picture, language, characters, …
02/08/19 3
Autumn 2019 CS101@CSE IIT Bombay
Picture Representation and Reconstruction
0 0 0 1 1 1 0 0 0 0
0 0 1 0 0 0 1 1 0 0
0 1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 1 0
1 0 1 0 0 0 1 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 1 1 1 0 0 1 0
0 1 0 0 0 0 0 0 1 0
0 0 1 0 0 0 1 1 0 0
0 0 0 1 1 1 0 0 0 0
(a) (b) (c)
Autumn 2019 CS101@CSE IIT Bombay
radix-based number systems
• Number systems with radix r has r symbols (including 0)• (x)r x is a string of symbols, r is the radix/base• key idea: position of a symbol determines it's value!
PLACE VALUE– Multiply from right to left by: r0, r1, r2, r3
, ... and then add
• decimal number system (r=10)• hexa-decimal number system (r=16)• binary (r=2)
02/08/19 5
Autumn 2019 CS101@CSE IIT Bombay
examplesdecimal hexa-decimal binary
26
0xf
255
16
0xffff
02/08/19 6
Autumn 2019 CS101@CSE IIT Bombay
examplesdecimal hexa-decimal binary
26 1a (0x1a) 10110
15 0xf 1111
255 0xff 11111111
16 0x10 00010000
65535 0xffff 1111111111111111
02/08/19 7
Autumn 2019 CS101@CSE IIT Bombay
number representation with computers
• all numbers represented in binary form• data types:
– int (32 bits)– unsigned int (32 bits)– float (32 bits)– double (64 bits)
• binary sequences in each have different formats
02/08/19 8
Autumn 2019 CS101@CSE IIT Bombay
two’s complement representation for integers
• For a n-bit integer (numbers to be stored in n bits)
• If x is positive: (0 <= x <= 2n-1 – 1)– Binary form of x
• If x is negative ( -2n-1 <= x < 0)– one’s complement of positive number + 1– E.g. -9 in 2's complement:
one’s complement (00001001) + 1 = 11110110 + 1 = 11110111– Binary form of 2n + x
100000000 - 000001001 = 11110111 = 247 decimal
Autumn 2019 CS101@CSE IIT Bombay
the two’s complement wrap-around
02/08/19 10
Zero is one position to the right of center
-1=1…1 0=0…0 Max=01…1Min=10…0
Autumn 2019 CS101@CSE IIT Bombay
why two’s complement?• single representation for zero• addition is same as subtraction!• examples:
8 + 4
8 - 4
4 - 8
02/08/19 11
Autumn 2019 CS101@CSE IIT Bombay
The bool type
• Old C++ used int to store Boolean values• But ANSI standard C++ offers a type called bool• bool tval = true, fval = false;• int ival = int(tval);• However, old bad habits still allowed
– if (37) { … }– bool bval = 37;
• Overall value unclear
02/08/19 12
Autumn 2019 CS101@CSE IIT Bombay
Bit array manipulation
• Fixed size integers are arrays of bits
• C++ lets you do bitwise Boolean algebra
• a & b (and), a | b (or), a^b (exor), ~b (not)
02/08/19 13
Autumn 2019 CS101@CSE IIT Bombay
Bitwise operators
• a & b (and), a | b (or), a^b (exor), ~b (not)
02/08/19 14
101101101001010110010100
&
101101101001010100100011
^
101101101001010110110111
|
0010001111011100~
Autumn 2019 CS101@CSE IIT Bombay
Left shift operator
• int c = 5; cout << (c << 2);
• Bits lost from the left (msb)• Zero bits inserted from the right (lsb)• Result is 20 (= 5 ´ 22)• Cheap way to multiply by powers of two
02/08/19 15
00000000,00000000,00000000,00010100
00000000,00000000,00000000,00000101
Autumn 2019 CS101@CSE IIT Bombay
Right shift operator
• c >> 2• Bits discarded to the right (lsb)• If msb of c was 0, then 0 bits injected from left
(msb)– 5 >> 2 gives 1
• If msb of c was 1 (c was negative) then 1 bits injected from left– -5 >> 2 gives -2 (work it out)– 0xfffffffb >> 2 gives 0xfffffffe
• Preserves sign of number02/08/19 16
Autumn 2019 CS101@CSE IIT Bombay
examples• generate powers of 2 using shift operator
• outputs of …int a = 1;cout << a;a = a << 31;cout << a;a = a >> 31;cout << a;
02/08/19 17
Autumn 2019 CS101@CSE IIT Bombay
Some applications of bit operations
• Is an int x odd or even?
• Remainder when divided by 8•
How many one bits in a 32-bit int?
02/08/19 18
Autumn 2019 CS101@CSE IIT Bombay
Some applications of bit operations
• Is an int x odd or even?– int isOdd = (x & 1);
• Remainder when divided by 8– int remain = (x & 7);– Faster than x % 8
• How many one bits in a 32-bit int?• Repeat 32 times:
– numOnes = numOnes + (x & 0x8000000);– x = x << 1;
02/08/19 19
In binary this looks like a one followed by 31 zeros
Autumn 2019 CS101@CSE IIT Bombay
fractions in binary
8 4 2 1 1/2 1/4 1/8 1/16
02/08/19 20
• powers on the right side of the point are negative:
• binary 0.1 = 0 + 1 x 2-1 = 0.5 in decimal
• in binary 0.11 = 0x 1 + 1 x 2-1 + 1 x 2-2
= 0.5 + 0.25 = 0.75 in decimal
Autumn 2019 CS101@CSE IIT Bombay
fractions and precision• represent decimal number 1.45 using 3 bits and
4 bits
02/08/19 21
Autumn 2019 CS101@CSE IIT Bombay
fractions and precision• represent decimal number 1.45 using 3 bits and
4 bits
• with 3 bits: 1.11 => 1.375• with 4 bits: 1.111 => 1.4375
• for actual storage as numbers all factions converted to mantissa + exponent form
• length of mantissa determines precission!
02/08/19 22
Autumn 2019 CS101@CSE IIT Bombay
Representing floating point numbers• Use an analogue of scientific notation:
• significand * 10exponent, e.g. 6.022 * 1022
• Significand (with a bit for sign) and exponent are in binary• significand * 2exponent
• Actual representation: more complex. “IEEE Floating Point Standard”
Costs how many bits to store
No of bits for mantissa
No of bits for exponent
float 32 24 8double 64 52 12
Autumn 2019 CS101@CSE IIT Bombay
example• representation of number 65 as a float• 65 in binary is 1000001• 65 in significand—exponent form
– 6.5 x 101
– 1000001– 1.000001 x 2110
– binary point moved 6 places to the right – 6 is 110 in binary
02/08/19 25
Autumn 2019 CS101@CSE IIT Bombay
example, number 65 as a float• 65 decimal in binary signifand-exponent form
– 1.000001 x 2110
• For float, – 23 bits for mantissa (+1 bit for sign)– 7 bits for exponent (+1 bit for sign)
• 65 decimal is stored as a float as follows,– decimal point is assumed after 2nd bit– 01000001000000000000000000000110
02/08/19 26
sign of mantissa sign of exponent
Autumn 2019 CS101@CSE IIT Bombay
more examples• 36 as a float
• 3.75 as float
02/08/19 27
Autumn 2019 CS101@CSE IIT Bombay
more examples• 36 as a float• 36 is 100010 in binary• 1.00010 x 2101
in binary significand (mantissa)-exponent form• 01000100000000000000000000000101
02/08/19 28
Autumn 2019 CS101@CSE IIT Bombay
more examples• 3.75 as a float• integral part is 3: 011 in binary• fractional part is .75 which is .11 in binary
– 1*(1/2) + 1 * (1/4) = 0.5 + 0.25 = 0.75• 3.75 decimal is 11.11 binary• convert to significand-exponent form
– 1.111 x 21
– Store in 32-bit float
02/08/19 29
Autumn 2019 CS101@CSE IIT Bombay
more examples• 3.76
• 0.75
• 3450– 1.10101111010 x 21011
02/08/19 30
Autumn 2019 CS101@CSE IIT Bombay
more examples
• Let us represent the number 3450 = 3.45 x 103
• First: Convert to binary:
• 3450 = 211+ 210+ 28 + 26+ 25+24 +23 + 21
• Thus 3450 in binary = 110101111010
• 3450 in significand-exponent notation: how?
• 1.10101111010 x 21011
− 10 in binary is 2 in decimal
− 1011 in binary is 11 in decimal, we have to move the
"binary point" 11 places to the right
− as float
01101011110100000000000000001011
11 10 9 8 7 6 5 4 3 2 1 0
1 1 0 1 0 1 1 1 1 0 1 0
Autumn 2019 CS101@CSE IIT Bombay
precision, big numbers• int, unsigned int, long, long unsigned int
• float, double, long double
• why does double have higher precision than float?
02/08/19 32
Autumn 2019 CS101@CSE IIT Bombay
different Needs, different Variable Types
• The keyword long : says, I need to store bigger or more precise numbers, so give me more than usual space.
• long unsigned int: Likely 64 bits will be allocated
• long double: likely 96 bits will be allocated
unsigned int telephone_number;
float velocity;
float mass, acceleration;
long unsigned int crypto_password;
long double more_precise_vaule;
02/08/19 33
Autumn 2019 CS101@CSE IIT Bombay
the Const Keyword
• const double pi = 3.14;
• The keyword const means : value assigned once cannot be
changed
• Useful in readability of a program
• area = pi * radius * radius;
• reads better than
• area = 3.14 * radius * radius;
Autumn 2019 CS101@CSE IIT Bombay02/08/19 35
Autumn 2019 CS101@CSE IIT Bombay
The Hardware(A very high level glimpse)
• How do we store numbers in hardware?• How is an instruction expressed in hardware?• How is it executed?• How do we output the numbers?
Autumn 2019 CS101@CSE IIT Bombay
Digital Circuits - Operations
• Building blocks of computers• Circuits have wires that carry current,
and are at a certain electric potential.
• Digital circuits: interpret electrical potential/voltage as numbers.
• Simplest convention− Voltage above 5 volt = number 1
− Voltage between 0 and 0.2 volt = number 0
− Circuit designed so that voltage will never be between 0.2 and 5 volt, hence no ambiguity.
Copyright © 1999-2000 Michael Stutz stutz@dsl.org
An inverter circuit
Autumn 2019 CS101@CSE IIT Bombay
Digital Circuits - Storage
• Capacitors (like batteries) can store electrical charges• Charge stored on a capacitor may also denote numbers
− Capacitor has low charge = value 0 stored on it.− Capacitor has high charge = value 1 stored on it.− Once charge is stored on a capacitor, it persists. Memory
• Building blocks of DRAMs (Dynamic Random Access Memory)
Autumn 2019 CS101@CSE IIT Bombay
Representing Numbers• How to represent numbers using this capability?• Key idea : Binary number system
– Represent all numbers using only 1's and 0's– Also called "Bits": "Binary digits"
Autumn 2019 CS101@CSE IIT Bombay
Representing Numbers
• Example: 25 Decimal = 11001 binary
– Use 5 capacitors
– Store high charge on 1st, 2nd, 5th,
and low charge on 3rd, 4th
• To transmit 25 from one part of the computer
to another
– Use 5 wires and raise the wires to appropriate
voltages at one end.
– Sense the voltages at the other end
Key idea:Store each bit of the number on a separate capacitor
Autumn 2019 CS101@CSE IIT Bombay
Bits, bytes, half-words, words
• Bit = 1 capacitor/wire• byte = 8 capacitors/wires• half-word = 16 capacitors/wires• word = 32 capacitors/wires• double word = 64 capacitors/wires
Autumn 2019 CS101@CSE IIT Bombay
Organization of a computer
Autumn 2019 CS101@CSE IIT Bombay
Memory
• Organized into bytes (groups of 8 capacitors)
• Memories of present day computers contain few Gigabytes, Giga=230
• Each byte in the memory is assigned a distinct number, or an address
• In a memory with N bytes, the addresses go from 0 to N-1
0 0 0 0 1 1 0 1 01
2
3
4
5 1 0 1 1 1 1 0 1
6
7
8
9
Autumn 2019 CS101@CSE IIT Bombay
Memory Ports
• Memory has 3 ports: address port, data port, control port.
• Address port consists of log N wires. (N = number of bytes in memory)
• You can place numbers 0..N-1 on address port
• Control Port may be just 1 wire. – Wire = 0: Memory to perform read
operation.– Wire = 1: Memory to perform write
operation.• Data port will have w wires, where
w is a multiple of 8. Say w=8m.
Autumn 2019 CS101@CSE IIT Bombay
Write Operation• Control Port must be set to 1.• If A is placed on the address
port, and D on data port, then D will be stored in the m bytes starting at byte A.
• (Remember that the data port had 8m wires, and so m bytes are available on the data port)
Autumn 2019 CS101@CSE IIT Bombay
Write Operation
0 1
10010
11101000
"Write the number 11101000 (232) into the location number 10010 (18)
Autumn 2019 CS101@CSE IIT Bombay
Read Operation
• Control Port must be set to 0.
• If A is placed on the address port, the m bytes starting at byte A will appear on the data port
• (Data port has 8m wires, and so m bytes will fit on the data port)
Autumn 2019 CS101@CSE IIT Bombay
Read Operation
1
10010
11101000
"Read from the location number 10010 (18)0
Autumn 2019 CS101@CSE IIT Bombay
Arithmetic Unit
• Ports: Input1, Input2, Output, Control• Typically Input1, Input2, Output will consist of w wires,
w = size of memory data port• Control = several wires. Number appearing on the control
wires will say what operation should be performed• 1 cycle after values are placed on Control, the Output will take
the commanded value: sum, product, …
Autumn 2019 CS101@CSE IIT Bombay
Peripherals: keyboard, screen, disk…
• Also have control port and data port like organization.
• Depending upon value placed on control port, the peripheral decides what to do with the value on the data port, or itself places values on the data port.
data
control
data
control
control
data
Autumn 2019 CS101@CSE IIT Bombay
Control Unit• Tells other parts of the computer what to
do.– Sends numbers on control wires of each unit
• The control unit decides what to tell other units by reading a “machine language program” from the memory of the computer.
Autumn 2019 CS101@CSE IIT Bombay
Machine language program
OPCODE OPERAND1 OPERAND2 OPERAND3
• Program = Sequence of instructions• Instruction = sequence of numbers
– First number is OPERATION CODE (OPCODE). This is the code that tells the Control Unit what OPERATION should do.
– Subsequent numbers are OPERANDS. These are "arguments" to the operation
• i386• x86_64• PowerPC
Autumn 2019 CS101@CSE IIT Bombay
Example57 100 200 300
• operation code 57 might mean:– Interpret the 3 numbers following 57 as addresses.– Read the words at the first two addresses and send them
to the Arithmetic unit.– Command the arithmetic unit to perform multiplication by
sending appropriate number on its control wires.– Store the result from the arithmetic unit into the word at
the third address
Autumn 2019 CS101@CSE IIT Bombay
Machine language progam• Program = Sequence of instructions• Instruction = sequence of numbers, first of which is an �operation code� which denotes what operation to perform
• Example: operation code 57 might mean:– Interpret the 3 numbers following 57 as addresses.– Read the words at the first two addresses and send them to the Arithmetic unit.– Command the arithmetic unit to perform multiplication by sending appropriate number on its control wires.– Store the result from the arithmetic unit into the word at the third address
• The sequence 57, 100, 200, 300 is an instruction that would cause the product of the numbers stored in addresses 100, 200 to be stored in the address 300.
• The operation codes are defined by the computer designer.
– She will assign a different code for each operation that she would like the computer to perform.– Example: 58 might mean the same thing as above, except that the numbers would be added.
Autumn 2019 CS101@CSE IIT Bombay
Control unit operation• Control unit must be told where the machine
language program is in memory
• The control unit then fetches the instructions constituting the program, interprets the codes, and performs the required operation
• After one instruction is fetched and executed, it fetches the next instruction and repeats the process
Autumn 2019 CS101@CSE IIT Bombay
Putting it together
Memory
Arthimetic Unit
Control Unit
Bus (network)
01..10...
100....
200
...300
57 100 200 300
10
1. Control unit is told the address of the instruction to fetch (e.g. instruction is at location 10)
2. A read operation is performed on memory location 10
3. instruction57 100 200 300is now "loaded" into the control unit
57 100 200 300
289
345
Read Write
1 0
Autumn 2019 CS101@CSE IIT Bombay
Putting it together
Memory
Arthimetic Unit
Control Unit
Bus (network)
01..10...
100....
200
...300
57 100 200 300
100
1. Now control unit performs a read operation of address 100
2. The number 289 is sent to input 1 of arithmetic unit
289
289
345
Read Write
1 0
Autumn 2019 CS101@CSE IIT Bombay
Putting it together
Memory
Arthimetic Unit
Control Unit
Bus (network)
01..10...
100....
200
...300
57 100 200 300
200
1. Now control unit performs a read operation of address 200
2. The number 345 is sent to input 2 of arithmetic unit
345
289
345
Read Write
1 0
Autumn 2019 CS101@CSE IIT Bombay
Putting it together
Memory
Arthimetic Unit
Control Unit
Bus (network)
01..10...
100....
200
...300
57 100 200 300
1. Now control unit instructs the arithmetic unit to perform a multiply operation (the "control" wires are set to the operation code of "multiply")
2. Control unitstores the product (289 x 345 = 99705) temporarily inside its own unit
289
345
Read Write
0 0
x
x 99705
99705
Autumn 2019 CS101@CSE IIT Bombay
Putting it together
Memory
Arthimetic Unit
Control Unit
Bus (network)
01..10...
100....
200
...300
57 100 200 300
300
1. Now control unit performs a write operation on address 300
2. The number 99705 is sent on the data port of memory and 300 is sent on the address port of the memory
99705
289
345
Read Write
0 1Instruction execution is COMPLETE
99705