Download as pdf or txt
Download as pdf or txt
You are on page 1of 59

CS 101:

Computer Programming and


Utilization

Puru
with
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 Autumn 2019 CS101@CSE IIT Bombay 2


why numbers?
• problem solving using computers is about
problem solving on numbers
• examples
– salaries, temperature, length, distance, voltage …
– picture, language, characters, …

02/08/19 Autumn 2019 CS101@CSE IIT Bombay 3


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 Autumn 2019 CS101@CSE IIT Bombay 5


examples
decimal hexa-decimal binary

26

0xf

255

16

0xffff

02/08/19 Autumn 2019 CS101@CSE IIT Bombay 6


examples
decimal hexa-decimal binary

26 1a (0x1a) 10110

15 0xf 1111

255 0xff 11111111

16 0x10 00010000

65535 0xffff 1111111111111111

02/08/19 Autumn 2019 CS101@CSE IIT Bombay 7


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 Autumn 2019 CS101@CSE IIT Bombay 8


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

Min=10…0 -1=1…1 0=0…0 Max=01…1

Zero is one position


to the right of center

02/08/19 Autumn 2019 CS101@CSE IIT Bombay 10


why two’s complement?
• single representation for zero
• addition is same as subtraction!
• examples:
8 + 4

8-4

4-8
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 11
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 Autumn 2019 CS101@CSE IIT Bombay 12


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 Autumn 2019 CS101@CSE IIT Bombay 13


Bitwise operators
• a & b (and), a | b (or), a^b (exor), ~b (not)

10110110 10110110
10010101 10010101
& ^
10010100 00100011
10110110 00100011
10010101 ~
| 11011100
10110111
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 14
Left shift operator

• int c = 5; cout << (c << 2);

00000000,00000000,00000000,00000101

00000000,00000000,00000000,00010100

• 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 Autumn 2019 CS101@CSE IIT Bombay 15
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 number
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 16
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 Autumn 2019 CS101@CSE IIT Bombay 17


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 Autumn 2019 CS101@CSE IIT Bombay 18


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;
In binary this looks like a one
followed by 31 zeros
02/08/19 Autumn 2019 CS101@CSE IIT Bombay 19
fractions in binary

• powers on the right side of the point are negative:

8 4 2 1 1/2 1/4 1/8 1/16

• 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

02/08/19 Autumn 2019 CS101@CSE IIT Bombay 20


fractions and precision
• represent decimal number 1.45 using 3 bits and
4 bits

02/08/19 Autumn 2019 CS101@CSE IIT Bombay 21


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 Autumn 2019 CS101@CSE IIT Bombay 22
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

Costs how No of bits for No of bits for


many bits to mantissa exponent
store
float 32 24 8
double 64 52 12

• Actual representation: more complex. “IEEE Floating Point


Standard”
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 Autumn 2019 CS101@CSE IIT Bombay 25


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

sign of mantissa sign of exponent


02/08/19 Autumn 2019 CS101@CSE IIT Bombay 26
more examples
• 36 as a float

• 3.75 as float

02/08/19 Autumn 2019 CS101@CSE IIT Bombay 27


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 Autumn 2019 CS101@CSE IIT Bombay 28


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 Autumn 2019 CS101@CSE IIT Bombay 29


more examples
• 3.76

• 0.75

• 3450
– 1.10101111010 x 21011

02/08/19 Autumn 2019 CS101@CSE IIT Bombay 30


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
11 10 9 8 7 6 5 4 3 2 1 0

1 1 0 1 0 1 1 1 1 0 1 0

• 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
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 Autumn 2019 CS101@CSE IIT Bombay 32


different Needs, different Variable Types

• The keyword long : says, I unsigned int


telephone_number;
need to store bigger or
more precise numbers, so float velocity;
give me more than usual
float mass, acceleration;
space.
• long unsigned int: Likely 64 long unsigned int
crypto_password;
bits will be allocated
• long double: likely 96 bits long double
more_precise_vaule;
will be allocated

02/08/19 Autumn 2019 CS101@CSE IIT Bombay 33


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 Bombay


02/08/19 Autumn 2019 CS101@CSE IIT Bombay 35
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
Copyright © 1999-2000 Michael Stutz stutz@dsl.org − Circuit designed so that voltage will
never be between 0.2 and 5 volt,
An inverter circuit hence no ambiguity.

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
Key idea:
Store each bit of the number on a
separate capacitor

• 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

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 0 0 0 0 1 1 0 1 0
of 8 capacitors) 1
• Memories of present day 2
computers contain few 3
Gigabytes, Giga=230 4
• Each byte in the memory is 5 1 0 1 1 1 1 0 1
assigned a distinct number, or 6
an address 7
• In a memory with N bytes, 8
the addresses go from 0 to N- 9
1

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

10010

11101000

"Write the number


11101000 (232) into
0 1 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

10010

11101000

"Read from the


location number
1 0 10010 (18)

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 data
organization.
• Depending upon value control
placed on control port, data
the peripheral decides
what to do with the control
value on the data port, data
or itself places values on
the data port. control

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
Example
57 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
Arthimetic 1. Control unit is told
Unit
the address of the
0
1 instruction to fetch
.. (e.g. instruction is at
10 57 100 200 300 location 10)
...
10
100 289 2. A read operation
.... is performed on
memory location 10
200 345 Bus
(network)
...
57 100 200 300
3. instruction
300 57 100 200 300
Control Unit
is now "loaded" into
Memory the control unit
1 0

Read Write

Autumn 2019 CS101@CSE IIT Bombay


Putting it together
Arthimetic 1. Now control unit
Unit
performs a read
0
1 operation of address
.. 100
10 57 100 200 300
...
100
100 289
2. The number 289
.... is sent to input 1 of
arithmetic unit
200 345 Bus
289 (network)
...
300
Control Unit

Memory

1 0

Read Write

Autumn 2019 CS101@CSE IIT Bombay


Putting it together
Arthimetic 1. Now control unit
Unit
performs a read
0
1 operation of address
.. 200
10 57 100 200 300
...
200 2. The number 345
100 289
is sent to input 2 of
.... arithmetic unit
200 345 Bus
345 (network)
...
300
Control Unit

Memory

1 0

Read Write

Autumn 2019 CS101@CSE IIT Bombay


Putting it together
Arthimetic 1. Now control unit
Unit
instructs the
0 99705
1 arithmetic unit to
.. x perform a multiply
10 57 100 200 300 operation (the
...
"control" wires are
100 289
set to the operation
.... code of "multiply")
200 345 Bus
(network)
2. Control unit
... stores the product
300 99705 (289 x 345 = 99705)
x Control Unit temporarily inside its
Memory own unit
0 0

Read Write

Autumn 2019 CS101@CSE IIT Bombay


Putting it together
Arthimetic 1. Now control unit
Unit
performs a write
0
1 operation on
.. address 300
10 57 100 200 300
...
300 2. The number
100 289
99705 is sent on the
.... data port of memory
and 300 is sent on
200 345 Bus
99705
(network)
the address port of
... the memory
300 99705
Control Unit

Memory

0 1
Instruction
execution is
Read Write COMPLETE
Autumn 2019 CS101@CSE IIT Bombay

You might also like