Full Transcript

NUMBER SYSTEMS SAFE PROGRAMMING Com Programmers puter Princ for iples News of the Week i Agenda 1. Base-10 programming in a binary world 2. Why do we care? because (100)10 == (01100100)2 (huh?)00 3. How can we use it? without making mistakes 4. Wh...

NUMBER SYSTEMS SAFE PROGRAMMING Com Programmers puter Princ for iples News of the Week i Agenda 1. Base-10 programming in a binary world 2. Why do we care? because (100)10 == (01100100)2 (huh?)00 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 d/load Value Name -bol Greek origin prefix Value at 1 Gbps chilioi 10001 thousand k kilo 210 = 10241 “thousand” 0.0000076 s 10002 million M mega 220 = 10242 mégas “great” 0.0078125 s 10003 billion G giga 230 = 10243 gígas “giant” 8 seconds 10004 trillion T tera 240 = 10244 teras 2.28 hours “monster” 10005 quadrillion P peta 250 = 10245 pénte “five” 97 days 10006 quintillion E exa 260 = 10246 héx “six” 272.4 years 10007 sextillion Z zetta 270 = 10247 septem “seven” 2,789 C 10008 septillion Y yotta 280 = 10248 oktṓ “eight” 285,616 M 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 2 7 2 6 2 5 2 4 23 2 2 2 1 2 0 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 '|' ASCII 124 '|' ASCII 124 'q' ASCII 113 '}' ASCII 125 '}' ASCII 125 'r' ASCII 114 '~' ASCII 126 '~' ASCII 126 's' ASCII 115 '⌂' ASCII 127 '⌂' ASCII 127 't' ASCII 116 'Ç' ASCII 128 'Ç' ASCII - 'u' ASCII 117 'ü' ASCII 129 128 'v' ASCII 118 'é' ASCII 130 'ü' ASCII - 'w' ASCII 119 'â' ASCII 131 127 'x' ASCII 120 'ä' ASCII 132 'é' ASCII - 'y' ASCII 121 126 'z' ASCII 122 signed 'â' ASCII - '{' ASCII 123 125 unsigned 'ä' ASCII - 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 calculations.  use only signed integer data types and if (thisInt == thatInt) { } 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

Use Quizgecko on...
Browser
Browser