4. 4 of 45Module 5 : Numbers and Built‐in Functions
Memory: Bits and Bytes
• 1 Byte = 8 bits (each bit either 0 or 1)
• 1 Byte has 28 different variations, i.e., 256
• How about 4 bytes?
0
0
0
0
0
0
0
0 0
0
00
1
11
1
1
1
1
1
1
1111000
1001
1002
Contents of
memory
Memory
This is also covered in
Introduction to Computing Systems
5. 5 of 45Module 5 : Numbers and Built‐in Functions
Integers (others)
• In most programming languages, an
integer takes up 4 bytes. The first bit for
sign: +ve or -ve, i.e., 31 bits left
• Since there is a zero needed to be
represented (as all zeros in the 32 bits),
the available data range is [-231, +231 – 1 ]
7. 7 of 45Module 5 : Numbers and Built‐in Functions
Float
• For real numbers with decimals
• Limited precision because we use limited
number of bits to store the data in a special
format (you will learn later in other course):
• Max. representable finite float
• Min. positive normalized float
Want to know more? http://docs.python.org/tutorial/floatingpoint.html
http://en.wikipedia.org/wiki/Double_precision_floating-point_format
10. 10 of 45Module 5 : Numbers and Built‐in Functions
Integer VS Float
• For the followings, which type is
more appropriate?
• Age
• Height
• Volume of a container
• Exchange rate
• Voltage and current
• Number of students in a class
• Bank account balance
12. 12 of 45Module 5 : Numbers and Built‐in Functions
What are Built-in functions
• Built-in functions are subroutines, procedures,
or methods that are built into Python itself; each
provides a specific functionality
• See the Python standard library:
http://docs.python.org/library/functions.html
• In fact, you knew some of them already:
• print (for displaying data)
• input (for reading user input)
• float, int, str (for converting data)
• type (for checking data type)
• exec (for executing a string in Python), …, etc.
13. 13 of 45Module 5 : Numbers and Built‐in Functions
• abs(x) – Return absolute value of x
• min(x,y) and max(x,y) – Return minimum
and maximum of x and y, respectively
They allow two or
more arguments
Let’s learn more: calling & arg(s)
Takes one argument
15. 15 of 45Module 5 : Numbers and Built‐in Functions
More Built-in Functions
• math.pow(x,y) – Return x raised to the power y
• math.sqrt(x) – Return the square root of x
• math.log(x) – Return natural logarithm of x
• math.log10(x) – Return base-10 logarithm of x
Math functions
usually return
“float”
16. 16 of 45Module 5 : Numbers and Built‐in Functions
More Built-in Functions
• math.sin(x), math.cos(x), math.tan(x)
– Return the sine, cosine, and tangent of x (radian)
• math.asin(x),math.acos(x),math.atan(x)
– Return arc sin, cos, and tan of x in radian
(Note: they all use radian!)
• math.degrees(x) and math.radians(x)
– Return x in degrees and radians, respectively
20. 20 of 45Module 5 : Numbers and Built‐in Functions
So… how to check?
• First, we can define a relatively small floating
point value, usually called epsilon or tolerance
• IDEA: if the absolute difference between the
two floating point numbers is smaller than
epsilon, we say that their values are the same
Key idea!!!!
(computationally but not mathematically)
23. 23 of 45Module 5 : Numbers and Built‐in Functions
Algorithm: compute square root
• We can compute the square root of a value,
say x, iteratively using the following idea:
Given x,
first compute an initial guess: guess = x/2
then diff = guess* guess - x
if abs(diff) < epsilon, we are done
if diff > 0, guess is too large
if diff < 0, guess is too small
update guess so that it gets closer, then
Repeat
24. 24 of 45Module 5 : Numbers and Built‐in Functions
Algorithm: compute square root
• But… how to update guess so that it gets
closer in every iteration…
• Let’s check x/guess…
In case x=100 and guess=50,
x/guess = 2
the targeting square root value should always
lie between guess and x/guess!
• So… we may update guess as mean value of
guess and x/guess and make it closer!
25. 25 of 45Module 5 : Numbers and Built‐in Functions
Algorithm: compute square root
Let’s do this test…
• Iteration #1
x=100 and guess=50
x/guess=2 -> new guess = mean = 26
• Iteration #2
x=100 and guess=26
x/guess=3.846 -> new guess = mean = 14.923
• Iteration #3
x=100 and guess=14.923
x/guess=6.701 -> new guess = mean = 10.812
…………
Really getting closer and closer!!!
34. 34 of 45Module 5 : Numbers and Built‐in Functions
NOT~
SHIFT (right)>>
SHIFT (left)<<
Bitwise XOR^
Bitwise OR|
Bitwise AND&
meaningoperator
What are Bitwise operators
• Python provides bitwise operators that process
input integers bit by bit correspondingly
• Treat 1 as true and 0 as false
Input:
15 -> 0000 1111
17 -> 0001 0001
What is 15 | 17 ?
All are binary
operations except ~,
which is unary
35. 35 of 45Module 5 : Numbers and Built‐in Functions
00 & 0
00 & 1
01 & 0
11 & 1
ResultAND
Truth tables of &, |, ^, ~
00 | 0
10 | 1
11 | 0
11 | 1
ResultOR
00 ^ 0
10 ^ 1
11 ^ 0
01 ^ 1
ResultXOR
• XOR is called exclusive or; it is true ONLY if one
of the two bits is true (but not both)
• ~x is a special operation called 2’s complement,
which is just -x-1, i.e., ~2 gives -3
40. 40 of 45Module 5 : Numbers and Built‐in Functions
Application #1
• xor can be used for doing simple encryption
and decryption
• Given
D – our data (any integer, i.e., 32 bits)
K – our key in encryption (which is an integer)
• We can perform encryption by:
E = D xor K where E is the encrypted integer
• and decryption by
E xor K gives D
We can recover D from E using K
XOR has this interesting property
Why? Study the truth table…
41. 41 of 45Module 5 : Numbers and Built‐in Functions
Application #2
• Pseudo Random Number Generator (PRNG):
• Seed – A number to initialize the generator
• Random number sequence – Use seed as
input, we apply the generator to obtain the first
random number; then use it as input to
generate the next random number, and so on…
See http://docs.python.org/library/random.html
• In designing a PRNG, bitwise operations are
often used, see next slide for a simple example
Interesting article:
http://spectrum.ieee.org/semiconductors/processors/behind-intels-new-randomnumber-generator
42. 42 of 45Module 5 : Numbers and Built‐in Functions
Application #2 (cont.)
• Example: Define a function “parityOf” to
compute the parity of an integer:
A bit to add to an integer to make the
number of 1s in its binary form to be even
Define a simple 12-bit PRNG
A sequence of random number
(binary numbers) can be
generated starting from the seed
note: 0x indicates
hexidecimal format
43. 43 of 45Module 5 : Numbers and Built‐in Functions
Application #2 (cont.)
• Note:
• You will learn how to use Python keyword “def” to
define functions later in this course
• Using bitwise operators, we can efficiently compute the
parity and develop pseudorandom number generators.
There are many PRNGs in the world; some are more
complicated, and they could have different quality…
• Since the numbers generated in the example code
form a binary number sequence, we also call it a
Pseudo Random Binary Sequence Generator (PRBSG)
• Related courses in the future:
“Computer Organization and Architecture” and
“Microprocessor-based System Design”
44. 44 of 45Module 5 : Numbers and Built‐in Functions
Take Home Messages
• Integers, Long, and Floating point numbers
– Representation and property: data range, minimum and
maximum values, and precision
• Built-in functions are useful resources built into
Python itself
• Comparing floating point numbers can be tricky
(and error-prone)
– Use absolute difference against a small epsilon to test
equality for floating point numbers