CP4P_NumberSystems (4)
CP4P_NumberSystems (4)
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
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”.
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
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
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
----------------------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
• 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.
• 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.”
• 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)
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.
• 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.)
• 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.)