0% found this document useful (0 votes)
6 views

CP4P_NumberSystems (4)

Uploaded by

Manu Bajwa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

CP4P_NumberSystems (4)

Uploaded by

Manu Bajwa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

NUMBER SYSTEMS

SAFE
PROGRAMMING
Com

Programmers
puter
Princ

for
iples
News of the Week i
Agenda
1. binary programming in a base-10 world
2. Why do we care?
because (100)10 == (01100100)2 (huh?)0
3. How can we use it?
without making mistakes
4. Why “Hexadecimal” numbers?
because (100)10 == (64)16 is easier than binary
Activity
• Need a calculator during today's lecture
• Work with integer sizes
• Identify and solve an integer overflow bug.
• Work with colours in both Decimal and Hex RGB
values.
Number Systems & Safe Programming

There are 10 types of people:


01 those who know binary
10 the other nine
11 those awaiting a ternary
joke
Number Systems & Safe Programming

There are 10 types of people:


01 those who know ternary
02 those who don't
10 those expecting
another binary joke
orders of magnitude of digital
Decimal Decimal Sym SI
data
Binary
Greek origin
d/load
Value Name -bol prefix Value at 1 Gbps
chilioi
10001 thousand k kilo 210 = 10241 0.0000076 s
“thousand”
10002 million M mega 220 = 10242 mégas “great” 0.0078125 s
10003 billion G giga 230 = 10243 gígas “giant” 8 seconds
teras
10004 trillion T tera 240 = 10244 2.28 hours
“monster”
10005 quadrillion P peta 250 = 10245 pénte “five” 97 days
héx
10006 quintillion E exa 260 = 10246 272.4 years
“six”
10007 sextillion Z zetta 270 = 10247 septem “seven” 2,789 C
1TB decimal = .90949TB binary 1TB binary = 1.0995TB decimal but don’t sweat it
Humans Computers
Analog Decimal Digital Binary
27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1

H        
M        
S        
Numbering Systems
How many ways can
you represent 2024?
2024 toothpicks
Numbering Systems
How much is 2024?
• One, two, three, many.
• MMXXIV
• 00000111 11101000
• 8:24 PM
Numbers, by themselves, are just…numbers. Context matters.
• The cardinal number 2,024 is different from the year 2024.
• Few or many depends on context: years, $$$, stars in galaxy
2024 = two millennia $2,024 = 1 PC 0.000002% of stars *
* before James Webb Tel. found at lot
more
8 bit byte (8×2)2 = 28 = 256
27 26 25 24 23 22 21 20
12
64 32 16 8 4 2 1
8
0 1 0 1 0 1 0 1
Is 01010101 char 'U' or decimal 85 ?
if ('U' == 85) is True.
Only binary matters to computers.
255

----------------------unsigned----------------------
Signed and
unsigned within a
single byte.

Integers
−/+ signed by
default
* unsigned by 0
Binary Processing Cautions
What happens when you have too many?

add 1
to max =
overflow
Binary Processing Cautions
What happens when you have too many for the data type?
• It doesn't matter. Data type must hold more than enough.
unsigned bits signed unsigned bits signed
127 01111111 127 255 11111111 −1
+1 00000001 +1 +1 00000001 +1
128 10000000 −128 0 00000000 0
overflow
But what happens when overflow has occurred?
• Overflow in C is undefined, therefore unpredictable, and
unstoppable -- program continues with binary values.
Binary is Binary, Context Matters
printf("'%c' ASCII %d", x, x);
unsigned/signed unsigned signed
'q' ASCII 113 '{' ASCII 123 '{' ASCII 123
'r' ASCII 114 '|' ASCII 124 '|' ASCII 124
's' ASCII 115 '}' ASCII 125 '}' ASCII 125
't' ASCII 116 '~' ASCII 126 '~' ASCII 126
'u' ASCII 117 '⌂' ASCII 127 '⌂' ASCII 127
'v' ASCII 118 'Ç' ASCII 128 'Ç' ASCII -128
'w' ASCII 119 'ü' ASCII 129 'ü' ASCII -127
'x' ASCII 120 'é' ASCII 130 'é' ASCII -126
'y' ASCII 121 'â' ASCII 131 'â' ASCII -125
'z' ASCII 122 'ä' ASCII 132 'ä' ASCII -124
Binary Processing Prudence
signed vs unsigned compares the binary values
results may be unexpected
unsigned bits signed unsigned vs signed
255 11111111 -1 255 == -1 is true 
255 > -1 is false 
255 < -1 is false 
• C# and Java do not have unsigned integers which are unsafe when
compared with signed integers or when mixed in arithmetic calculations.
 use only signed integer data types and
if (thisInt == thatInt) { /* will always be correct */
}
Bits and bit width
Know how many. Get a tattoo.
n bits encode 2n values = bit width
• 8-bit char = 28 = 256 values (unsigned) 0 – 255
• 16-bit short = 216 = 65,536 values from 32,767 to −32,768
• 32-bit long = 232 = 4,294,967,296 values (65,5362)
2,147,483,647 to −2,147,483,648
• 64-bit long long = 264 = 1,2,3,many but slow to process
‼ int short long data types are signed by default,
must declare unsigned int but don’t.
Binary Processing Problems
• Gangnam Style breaks
YouTube, Dec. 2014
• PSY - GANGNAM
STYLE( 강남스타일 )
• YouTube view counter
goes to 64-bit width
• > 4.6B views
since 15 July 2012
Binary Processing Problems
• If a counter tracks hundredths of a second,
how many days would it take for an integer to overflow?
• SHORT_MAX (32,767) = almost right away…we can guess that.
• LONG_MAX (2,147,483,647) = +R “calc”.

• The End is coming for UNIX: January 19, 2038 03:14:07


• UNIX epoch 1970-01-01 00:00:00 + LONG_MAX seconds = The End
• 32-bit Unix systems will go back in time 136.1 years due to overflow.
• On 2033-05-17, timestamps will reach 2,000,000,000 seconds.
Save the date for geek, nerd, and Unix user group parties!
Binary Processing Prudence
• What will never overflow on a 32-bit platform
can easily overflow on a 16-bit platform
• INT_MAX = 32,767 or 2,147,483,647 platform
INT_MIN = -32,768 or -2,147,483,648 dependent
• INT_MIN <= x+y <= INT_MAX // primary effect
• INT_MIN – 1 = INT_MAX // side-
effect
INT_MAX + 1 = INT_MIN // side-
effect
• Never program for a side-effect. Never ever be clever.
Binary Processing Prudence
C language implementation varies by compiler & platform
• C code could compile successfully but run differently.
• Data types have minimum sizes: platform's int width varies 8–64
• For portability, declare exact size/type you need <stdint.h>
• intnn_t = minimum 100 times bigger than the biggest value expected.
• For efficiency, declare int
• platform will use 16-bit or more but know the sizeof(int)
• Then test, test, test, test, test the edge cases
• gcc –std=c99 -pedantic -Wall -Wextra -Wshadow -Wconversion
-Wstrict-overflow=5 -Wsign-conversion -Wformat­=2 –Werror
• examine compiler warning messages
Floats and Doubles — fractions
In binary,
what is the value between 0 and 1?

A float or a double, when your program is run,


Can cause you great trouble before it is done.
Values can muddle, though it all seems like fun,
They float like a bubble, between zero and one.
Floats and Doubles — underflow
min.
scale

See a
1 / 1 = float 1.000000000000000000000000000 6
decimals
1 / 1 = double 1.00000000000000000000000000000000 10
decimals

pattern?
1 / 2 = float 0.500000000000000000000000000
1 / 2 = double 0.50000000000000000000000000000000
1 / 3 = float 0.333333343267440795898437500
1 / 3 = double 0.33333333333333331482961625624739
1 / 4 = float 0.250000000000000000000000000
Floats and Doubles — underflow
min.
scale

See a
1 / 5 = float 0.200000002980232238769531250 6
decimals
1 / 5 = double 0.20000000000000001110223024625156 10
decimals

pattern?
1 / 8 = float 0.125000000000000000000000000
1 / 8 = double 0.12500000000000000000000000000000
1 /10 = float 0.100000001490116119384765625
1 /10 = double 0.10000000000000000555111512312578
1 /16 = float 0.062500000000000000000000000
Floats and Doubles — fractions
In a binary system, what value is between 0 and 1?
• Every fractional value is an approximation between 0 and 1.
• Not to be used for monetary or exact values in business
• DECIMAL data types are integers with implied decimal positions
DECIMAL size 9.2 (precision.scale) contains 1,234,567.89
• BE VERY CAREFUL mixing integer and floating-point types
• Do not compare floats and doubles, only powers of 2 are ==
• In general, use doubles. Precision sacrifices scale as
significant digits increase in value. The decimal point floats!
• Test edge cases of expected highest/lowest values.
Floats and Doubles — fractions
How many programmers does it take
to change a lightbulb?

1.00000000000000111022302462515654042363
8203

Never use a float or double when a short or long will do.


Ariane 5 rocket
• 10 years and $7B
of development
• 1st flight June,1996
• Ariane 4 software
• Ariane 5 was faster
• 36 seconds of
normal flight
• at 36.7 seconds…
data conversion of

64-bit double to
16-bit integer
• at 37 seconds…
course correction
• at 39 seconds…
$500M loss
Programming goes wrong on
overflow
• Ariane 5 rocket self-destructs 40 sec after launch in 1996
• casting a 64-bit double to 16-bit int needs 275B int variables
• For 6 hours, over 6000 people got a busy signal calling 911
• 911 call routing stopped after a counter reached maximum
• Therac-25 radiation therapy device kills 6 (1985-1987)
• error++; If (error != 0) means do not irradiate patient
+1 to a byte 256 times equals zero. use isError set to true|false
• Toyota Sudden Unintended Acceleration kills ~400 (2002-2010)
• data type casting altered values. 30% of switch without error traps
Error Traps
IF (true1) { switch(char_variable) {
do this; case 'a':
} case 'A':
ELSE IF (true2) { do_this();
do that; // fall
} thru?!?!
ELSE { /* error trap */
case 'b':
unknownCondition =
case 'B':
true;
do_that();
}
break; // don’t
...
forget!
IF (unknownCondition) {
exit(86); // default: // error trap
stop!!!!!!! exit(86); //
Defensive Programming
• Know your data! Use great care when mixing data types.
• Check for type_MAX and type_MIN. Overflow means unknown.
• Consider overflow for each intermediate value in calculations
• mid = (low + high) / 2; // low + high can overflow before ÷ 2

+ − × operation
• Overflow can occur at each

• Code for people, not for machines (compilers can optimize for efficiency)
• Use comments; write code that reads easily.
• Use error traps: IF – THEN ends with unconditional ELSE
• Never ever be clever. Obfuscated C Code Contest
int main(int b,char**i){long long n=B,a=I^n,r=(a/b&a)>>4,y=atoi(*++i),_=(((a^n/b)*(y>>T)|
y>>S)&r)|(a^r);printf("%.8s\n",(char*)&_);}
Binary search  mid = (low + high) / 2 ; //
overflow bug
mid = (low + high) / 2
(intermediate value)

if (search >
array[mid]){
low = mid + 1
} else
if (search <
array[mid]){
high = mid - 1
} else {
// search =
array[mid]
}
Hexadecimal
• “Hex” is base 16
• Why? easier than
binary but related
• 8 bit byte holds
256 values.
16 * 16 = 256
thus Hex
• 0–15: One – Nine, Able, Baker, Charlie, Dog, Easy, Fox
Hex
• ASCII more accurately, US-ASCII
American Standard Code for Information Interchange
• 0000 1010 \n is 0x0A or Oh-Able
• 0111 1111 (127)10 is 0x7F or Seven-Fox
• 1111 1111 (255)10 is 0xFF or Fox-Fox
• HTML & URLs use HEX values for special characters
• 0010 0000 (32)10 is 0x20 or %20 = space char
HTML uses Hexadecimal color codes
• Hex codes of RGB triplets are used in HTML
colour representations, as in: #XXXXXX
• Red, Green, and Blue values range
from 00 (lowest intensity)
to FF (highest intensity) in HEX
• FFFFFF is white (screen emits all colours)
– presence of all visible wavelengths
• 00000 is black
– absence of any visible wavelength
• eLearning: Hex numbers
• eLearning Tutorials Login needs your Seneca
credentials and a LinkedIn account
Notes

Not on the quiz


but worth having
an appreciation for…
Binary: Zero | One, On | Off, True | False
Leibniz (17th C) codifies the binary system
• 0 is the void, 1 is God.

Bit = Binary digit


• unambiguous and unequivocal unit used
to encode a message for transmission
• (Claude Shannon, 1948. Bell Labs)

Byte = 8 bits − the smallest unit of data


• 256 possible values { 0 – 255 }
• (W. Buchholz, 1956. )
Why Binary?
Unambiguous and Unequivocal state:
• 0 or 1 for storage of a value in a bit
• Electric current in RAM, magnetic polarity on HDD, magic in SSD
• Computer switches/storage reliably hold only two possible states
• TRUE or FALSE to make a decision
• hardware uses logic gates, software uses Boolean logic
• All logic uses True | False  there is no maybe

Faster Processing:
• CPUs, ALUs, FPUs, GPUs run better in binary
Boolean Logic: Operator Truth
Tables
If wishes were horses, beggars would ride.
Can you ride?
x — are wishes horses? y — have your own real horse?

x NOT x x y x AND y x y x OR y

T T T T T T
T F
T F F T F T

F T F F T T
F T
F F F F F F
Safe, but slow, integer
arithmetic
#include <limits.h>
int safe_add(int a, int b) int safe_mult(int a, int b)
{ { // efficiency: abs(a)>=abs(b)
if (a >= 0) { // positive if (a == 0 || b == 0)
if (b > (INT_MAX - a)) { { return 0; }
int x = a;
/* handle overflow */
for (int i=2;i<=abs(b);i++)
}
{
} else { // negative
x = safe_add(a,x);
if (b < (INT_MIN - a)) {
}
/* handle overflow */
if ( b < 0 ) { x = x * -1 }
} return x;
} }
return a + b;
}
Bad Programming goes wrong
• Ping of Death crashes small computers/servers in 1995/6
• No validity checking on fragmented IP packets allows reassembly
to exceed max. packet length (16-bit value) causing buffer overflow
• Mars climate orbiter crashes in 1999
• USC/Imperial versus Metric contest ended in a split decision
• Antarctica's ozone layer hole discovered 7 years late
• NASA software ignored values outside range of expected
measurements. Tell-tale data was ignored from 1978 – 1985.
• Lack of Random seeding could win you $1 million
• Don't use rand() without first seeding with srand(NULL)
Numbering Systems

Humans tend to think…like humans.


Numbering Systems

We can think differently.


1 of 2
2 of 2
255
Two's

----------------------unsigned----------------------
Complement
+127 01111111 max
positive
−128 10000000 min
negative
= −1 11111111 correct
+3 00000011 matching
−3 11111101 pos and
neg
= 0 00000000 always
zero

+127 01111111 max


positive
+1 00000001 add one 0
−128 10000000 overflow
Floats and Doubles — integer precision
float Single-precision floating-point (32-bit base-2)
• Precision: 24 bits left & right of decimal, 6 decimals accuracy
• Precision limit: +/- 16,777,216 exact integer values, 224
double Double-precision floating-point (64-bit base-2)
• Precision: 53 bits left & right of decimal, 15 decimals accuracy
• Precision limit: +/- 9,007,199,254,740,992 (9 quadrillion)
• double calcs take longer to run but are safer than float
• Actual properties of float and double are unspecified in C
What is a Number System?
• The “information stored and processed inside a computer system is represented in
machine language which is only about 0s and 1s.”

• Therefore, the memory contents (which are “all digits and numbers”) would be different
from what we type (the strings of letters as an ex.) and we should have some ways to
present them.

• A Number System is a “writing system for expressing numbers;” It is a “mathematical


notation for representing numbers of a given set,” using digits or other symbols in a
consistent manner.

• A Number System could also referred to as “ a numeral system” or “system of


numeration.” In this lecture, we will look at most common Number Systems.
Why do we need different number
systems in computing? And Common
Number Systems
• A computer system “only understands numbers.” Therefore a computer “translates” all
input including letters, words, special characters to numbers.

• While we, as human beings, use our “Decimal” number system with 10 (0 - 9) digits, a
computer understands “Binary” number system that has only 2 (0 and 1) digits. Different
number systems are just “different views to same numbers, but in different systems.”

• To represent binary numbers more concisely, we use “Hexadecimal” number system


that uses 10 digits (0 – 9) and 6 symbols (A – F).

• Each of these digits or symbols, represent different values “depending on their position
in a given number.” – more on this soon.
Common Number Systems – Value of
Digits in a Number
• For each of the number systems mentioned, “the value of a digit in a
number” is determined by:

1. “The digit itself.” (As an example in decimal, each of the digits 0 - 9 have
different values)

2. “Position of the digit.” (As an example in decimal: 10s, 100s, 1000s,


10000s, etc.)

3. “The base of the number system.” (which is its total number of digits.)
Common Number Systems – Decimal Number
System
• Decimal Number System is the one which is “human preferred” and the one
we use “in everyday life.”

• “It has base 10 as it uses 10 digits (0 – 9).” The “values of digits increase
from right to left:” 1s, 10s, 100s, 1000s, etc.

• “Each successive position represents a specific power of base 10.” Take


1234 as an example. It has 4 in ones position, 3 in tens position, 2 in
hundreds position, and 1 in thousands position and could be expressed as:
1234 = (1 x 103) + (2 x 102) + (3 x 101) + (4 x 100)
= (1 x 1000) + (2 x 100) + (3 x 10) + (4 x 1)
Binary number system
• Binary number system is “the one that a computer or machine understands.”

• It uses only two digits: 0 and 1, which means “its base is 2” (or it’s a Base 2
number system.)

• Like Decimal, “the values of digits increase from right to left” and “each successive
position represents a specific power of base” (which is 2 in this case.)

• Take (1001)2 as a binary number example. It could be expressed as:


(1001)2 = (1 x 23) + (0 x 22) + (0 x 21) + (1 x 20)
= (1 x 8) + (0 x 4) + (0 x 2) + (1 x 1) = (9) 10
Importance of Binary number system
• “All data transfer, storage, and processing done by a microcomputer is
performed digitally using binary” (base 2) codes.

• This binary system “translates every character entered in the computer


into a set of 1's and 0's.” As an example, computer represents the letter
‘A’ as 1000001 (or the code 65 in decimal.)

• The advantage of binary coding over other methods is that “a sequence


of only two possible states is required to represent a character in the
electronic circuits of the computer.” Therefore, the smallest piece of
information that needs to be stored in memory is a single binary digit.
Importance of Binary number system (Cont’d)
• A single binary digit is called a “bit.” Different groupings of bits are
used to represent different characters. A collection of eight bits is
called a “byte.”

• “One byte can represent any of 256 characters” (28 =256). As an


example, the word "bit" would require a total of three bytes of
memory, one byte for each character in the word.

• Since we are primarily concerned with how many characters the


memory of a computer can hold, “memory size is referred to in
units of bytes.”
Importance of Binary number system (Cont’d),
and How to convert from Decimal to Binary and
vice versa
• In binary arithmetic, the power of 2 that is closest to 1000 is 2 10 (210 = 1024).
Therefore, “in computer jargon, the prefix kilo stands for 1024.” Frequently, the
word kilobyte is abbreviated “kB”. (Note the “k” is lower case because “K” in metric
is reserved for the Kelvin temperature scale.)
• a chip with 256kB of RAM can store 262,144 characters in memory
(256 X 1024 = 262,144).
• eLearning: binary and bits, binary numbers
• eLearning Tutorials Login needs your Seneca credentials and a LinkedIn account since LinkedIn
bought Lynda
Hexadecimal number system
• Hexadecimal (or base 16 number system) uses “16 digits: 0 to 9, and A to F.”
Letters A to F represent digits starting from 10 (A = 10, B = 11, C = 12, D = 13, E =
14, F = 15.)

• Like other number systems, “the values of digits increase from right to left” and
“each successive position represents a specific power of base” (which is 16 in this
case.)

• Take (19FD)16 as a hexadecimal number example. It could be expressed as:

(19FD)16 = ((1 x 163) + (9 x 162) + (F x 161) + (D x 160))


= (1 x 4096) + (9 x 256) + (15 x 16) + (13 x 1) = (6653) 10

You might also like