ecology labfaculty.cse.tamu.edu/slupoli/notes/csoverview/numbe… · web viewbinary, you have only...

37
Numbering Systems and Binary Math The overall concept CS majors must understand the basic numbering systems and WHY (explained later) they are used. The basic numbering systems are o Binary (base 2) o Decimal (base 10) o Octal (base 8) o Hexadecimal (base 16) Base means how many character options you have o binary, you have only two options ( base 2 = 0 – 1) o options usually START at 0 none of the character used in representing the numbers are negative o which is an issue, we have to devise a plan for this If you have an understanding of the number systems, you will need to use and understand them for the following topics in computer science o how to calculate network addresses o how to read memory address locations in core dumps o understand the color definitions in web and application programming o understand many other concepts

Upload: others

Post on 30-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

Numbering Systems and Binary Math

The overall concept CS majors must understand the basic numbering systems and WHY

(explained later) they are used. The basic numbering systems are

o Binary (base 2)o Decimal (base 10)o Octal (base 8)o Hexadecimal (base 16)

Base means how many character options you haveo binary, you have only two options ( base 2 = 0 – 1)o options usually START at 0

none of the character used in representing the numbers are negativeo which is an issue, we have to devise a plan for this

If you have an understanding of the number systems, you will need to use and understand them for the following topics in computer scienceo how to calculate network addresses o how to read memory address locations in core dumps o understand the color definitions in web and application programming o understand many other concepts

Page 2: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

Decimal most common what we use everyday is a base 10 system meaning that each digit is powered by an increasing value

with the base digit of 10 possible digit values (10)

o 0 - 9

Decimal Example

but why is it used?o what we were taught no matter the cultureo used in many NON-MATH applications

phones addresses etc…

Page 3: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

Binary used in many computer science applications

o electronics (0 = off, 1 = on)o decisions (0 = false, 1 = true)

is a base 2 system meaning that each digit is powered by an increasing value with the base digit of 2

possible digit values (2)o 0 - 1

Binary Example

Order and Representation of Decimal values in Bits128 64 32 16 8 4 2 1 Decimal

0 0 0 0 0 0 0 0 = 00 0 0 0 0 0 0 1 = 10 0 0 0 0 0 1 0 = 20 0 0 0 0 0 1 1 = 30 0 0 0 0 1 0 0 = 4

… = …

Page 4: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

# 128 64 32 16 8 4 2 1 Decimal0 0 0 0 0 0 0 0 = 00 0 0 0 0 0 0 1 = 10 0 0 0 0 0 1 0 = 20 0 0 0 0 0 1 1 = 30 0 0 0 0 1 0 0 = 4

ex. 0 1 1 1 0 0 0 0 =1 0 1 0 0 1 0 1 1 =2 1 1 1 1 1 0 0 0 =3 0 1 1 1 0 1 0 1 =4 1 0 1 1 0 0 1 0 =5 1 0 1 1 0 0 0 1 =6 1 0 0 0 0 0 0 0 =7 0 1 0 0 1 0 0 1 =8 0 0 0 1 0 1 1 0 =9 0 1 0 1 1 0 0 1 =10 1 0 0 0 1 1 0 0 =11 0 0 0 0 0 1 1 1 =12 1 0 1 1 0 1 0 0 =13 1 0 0 0 1 1 1 1 =14 0 1 0 1 0 1 1 1 =15 0 0 1 0 1 0 1 1 =16 0 0 0 1 1 0 1 1 =17 0 0 0 0 1 0 0 0 =# 128 64 32 16 8 4 2 1 DecimalEx. = 1171 = 862 = 1583 = 2014 = 1365 = 2506 = 1797 = 238 = 1009 = 20010 = 231

Page 5: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

Another way of converting a Decimal to Binary

But why is it used?o simplest to determine electrically

yes/no on/off

o combine these and it gets more powerful used in writing data to a hard drive

o the On/off’s are converted back to data (and vise versa when we save data)

o there has to be SOME type of conversion from the 1/0s to data to make sense

How 0s and 1s are so importantHow 0/1 are stored How they have a pattern

68 ÷ 2 = 34 R 034 ÷ 2 = 17 R 017 ÷ 2 = 8 R 1 8 ÷ 2 = 4 R 0 4 ÷ 2 = 2 R 0 2 ÷ 2 = 1 R 0 1 ÷ 2 = 0 R 1

1 0 0 0 1 0 0 = 68 (base 10)

Page 6: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

(Play Binary Bingo here!!)Hexadecimal

used in many computer science applicationso memory valueso color values (HTML)

is a base 16 system meaning there are 16 possible digit valueso uses a letter (A-F) to keep it a SINGLE character

Hexadecimal ValuesDecimal Hex

0 01 12 23 34 45 56 67 78 89 910 A11 B12 C13 D14 E15 F

Page 7: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

Hexadecimal Example160 = 1161 = 16162 = 256163 = 4096164 = 65536

But why is it used?o since HEX can range MANY more values than binary or decimalo used for determining color on a webpage

we can see (and a monitor can display) 16+ million colors!! from black to white and all shades and colors in between!!

How is Hexadecimal used?

Page 8: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

What code is white?What code is black?

Converting Decimal to Hexadecimal

Express the decimal numbers of the exercises below in hexadecimal notation.1. 1684 2. 742 3. 111

Express the hexadecimal numbers of the exercises below in decimal notation.1. E2 2. 987 3. FFF4. A4 5. CDD3 6. 4E8

Page 9: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

7. AA53 8. 12BB 9. 1AFF

Page 10: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

Octal used in many (some outdated) computer science applications

o memory valueso circuit boards/robotics

is a base 8 system meaning there are 8 possible digit values possible digit values (0-7)

Octal Example

But why is it used?

80 = 181 = 882 = 6483 = 51284 = 4096

Hexadecimal ValuesDecimal Octal

0 01 12 23 34 45 56 67 78 109 11

Page 11: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

o not many examples anymoreo old 8 bit systems (only had 8 inputs)o Octal is used as shorthand for representing file permissions on UNIX

systems.

Octal in an Application

-rw-r--r-- = 0 [1,1,0][1,0,0][1,0,0] = 0 6 4 4

Page 12: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

octal to decimal conversion

Express the decimal numbers of the exercises below in octal notation.1. 1684 2. 742 3. 111

Express the octal numbers of the exercises below in decimal notation.1. 72 2. 175 3. 1277

Page 13: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

Binary Mathematics - Addition Solved much like normal numbers Four rules to remember when solving:

o 0 + 0 = 0o 1 + 0 = 1o 1 + 1 = 0, plus 1 carry (carry over to the next number)o Carry + 1 + 1 = 1, plus 1 carryo value can spill to NEW digit!!! (Example 2)

Binary Addition ExamplesExample 1 Example 2101101110+ 1011010111001000

10111110+ 1011110100011100

Try these problems 1010+1111

111+110

101+111

111+111

1010+1010

11101+ 1010

11111+11111

1001+1000

Ans1. 110012. 11013. 1100

4. 11105. 101006. 100111

7. 111110

Page 14: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

Binary Mathematics - Subtraction Solved much like normal numbers when borrowing, it is considered 10

o 10(binary 10, not 10 decimal) – 1 = 1

How to Reduce in Subtraction

Binary Subtraction ExamplesExample 1 Example 2 10110- 01101 1001

1100110- 1011001 1101

Try these problems110- 10

101 - 11

1001- 11

1101- 11

10001- 100

111- 110

111- 111

1011011110- 1011011110

Answers:

1. 1002. 103. 110

4. 10105. 11016. 001

7. 0

Page 15: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

Issues with Binary Subtraction issues arise when the RESULT is > 0

o in solving, we need to reduce, but there isn’t anything left!! this issue will be solved using Two’s Compliment

Issues in Subtracting with a Negative resultScenario 1 Scenario 2

Page 16: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

Binary Mathematics – Multiplication Solved much like normal numbers

o 0 * 0 = 0o 0 * 1 = 0

o 1 * 0 = 0o 1 * 1 = 1

much like AND Gate!!!

Binary Multiplication ExamplesExample 1 Example 2 1100100 x 01011 1100100 11001000 000000000 11001000000000000000001101001100

10110111 x 111111 10110111 101101110 1011011100 10110111000 101101110000 101101110000010110100001001

Try these problems 10x 10

100x 11

101x 10

1011x 11

11011x 101

Answers:1. 1002. 1100

3. 10104. 100001

5. 10000111

Page 17: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

Binary Mathematics – Division Solving using long division!!! Remember to use BINARY subtraction!!! there are remainders!! (Examples, R 1, R 10, etc…)

Binary Division Examples

Example 1 Example 2 10101.1 10110 _________ __________10 | 101011.0 101 | 1101110 -10 101 010 111 -10 101 011 101 -10 101 10 00 10 0

Ex. #1 = 10101 R 1

Try these problems#1. 100 / 10 #2. 111 / 11 #3. 1010 / 100

#4. 1101 / 11 #5. 10111 / 10 #6. 11111/100

1. wers:102. 10 R 1

3. 10 R 104. 100 R 1

5. 1011 R 1

Page 18: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

Negative Numbers and Two’s Compliment remember how we couldn’t subtract in binary math is the result was negative? in dealing with this, we have to concern ourselves with a few things

o with the number of bits we are allowed (SIZE)o the layout of the overall storage of the bits

LSB = Least Significant Bit (always the rightmost bit) MSB = Most Significant Bit (always the leftmost bit)

MSB/LSB

we use two’s Compliment to show negative numberso MSB is always negative and the most (absolute) valueo specific algorithm

trigger MSB to 1 if the value is negative add others (those with 1) to negative MSB value

Algorithm to create negative numbers

What would be the MAXIMUM positive value in a 4 bit system?What would be the MINIMUM negative value in a 4 bit system? Answerb:

Page 19: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

Creating -3 in Twos Compliment (3 and 4 bit)

Two’s compliment algorithm version 2!!o invert all bitso add oneo then subtract

Complete the tables below for Two’s ComplimentValue (base 10)

321

0 0 0 0-1-2-3-4

Value7654321

0 0 0 0 0-1-2-3-4-5-6-7-8

Page 20: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

Adding(and -) numbers in Two’s Compliment same as we have before but

o MSB still has meaning 0 = positive number 1 = negative number

o size again matters, and carry is IGNOREDo In subtracting, here’s the trick, we’re going to ADD the negative

numbers together in two’s compliment, it still works out

Adding Two’s Compliment Example

Convert these to Two’s Complement then solve, all are in 4 bit systems:

5+ - 3

6 + - 1

4 + - 6

2 + -4

1+ - 5

Page 21: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

FYI – Section

Page 22: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

Representing Rational Numbers (Fixed) This will explain why the datatype FLOAT/DOUBLE is so much larger than

an INTEGER the overall game plan

o we cannot store a LITERAL dot in a floating point numbero we have to address

where the “dot” (called a radix) convert the fractional portion convert the decimal portion (easy) fit it all in a ??? bit system!!

Python

Data type Description and examples

integer signed integer, 32 bits, 2147483647

float 64 bit double precision, like 1.23 or 7.8e-28

careful, breakdown of the byte (8 bits) can be different the “.” is called a RADIX

o the radix allows us to use the 4 bits as optimally as possible radix “moves” left if more space for the decimal portion is needed radix “moves” right if more space for the integer portion is needed

o uses Exponent portiono this is in EXCESS notation!! (backwards of sign!!)o tells us where to move the “radix”

+ value in excess means move right - value in excess means move left

Page 23: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

Breakdown of the fractional byte

the sign portions o MSBo 0 = + 1 = -

Mantissao same as regular binaryo which will have the whole and fractional value

Review of Fractionsfractional decimal

halves ??/2 .50fourths ??/4 .25eighths ??/8 .125sixteenths ??/16 .0625thrityseconds ??/32 .03125

Order and Representation of Fractional values in Bits

Exp radix .50 .25 .125 .0625 .03125 … Decimal. 0 0 0 0 0 - = 0. 1 0 0 0 0 - = .50. 0 1 0 0 0 - = .25

Page 24: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

. 0 0 1 0 0 - = .125

. 0 0 0 1 0 - = .0625

. 0 0 0 0 1 - = .03125

. 1 1 0 0 0 - = .75

. 1 1 1 0 0 - = .875

. 1 1 1 1 0 - = .9375

. 1 1 1 1 1 - = .96875… = …

Representing Rational Numbers (Floating)

http://www.youtube.com/watch?v=t-8fMtUNX1A

Converting from Float to Binary the overall algorithm from Float to Binary

1. Determine the sign bit2. Convert the integer portion to binary3. Convert the fractional portion to binary4. Move the radix to the left of the most significant digit (left-most non-

zero digit)5. Determine the exponent in excess 3 notation

Converting +2 3/4 (2.75) to binary

1. This is positive so our sign bit is 02. 2 is 103. 3/4 is .114. This gives us 10.11

Move the radix two places to the left: .10115. So the exponent is +2: 110 in excess6. Giving us: 0 110 1011

Converting +0 3/4 (0.75) to binary

1. This is positive so our sign bit is 02. 0 is 003. 3/4 is .114. This gives us 00.11

Move the radix two places to the right: 0011.

Page 25: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

5. So the exponent is -2: 010 in excess6. Giving us: 0 010 1011

Solve:

1. -1.18752. +0.343753. 10111100 in decimal4. 01011010

Answersb:

Page 26: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

Negative Numbers and Excess Notation Simple MSB is key if +/-

o 1 = positiveo 0 = negative

SWITCHED!!! loose one bit to sign, rest are used normally

Excess Notation ChartRepresentation Value

000 -4001 -3010 -2011 -1100 0101 1110 2111 3

Page 27: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

Answer Section

Range of two’s compliment in a 4 bit system

Floating to Binary (and Vise Versa)

-1.1875

1. This is negative so our sign bit is 1 2. 1 is 1 3. .1875 is 3/16 giving us .0011 4. This gives us 1.0011: Because we have 4 bits we have to use 1.001: Move the radix one place to the left:

.1001 5. So the exponent is +1: 101 in excess 3 6. Giving us: 1 101 1001

+0.34375

Page 28: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

1. This is positive so our sign bit is 0 2. There is no whole number part 3. .34375 is 11/32 giving us .01011: We need four significant digits. The leading zero is not significant. 4. This gives us 0.1011. Move the radix point one place to the right: .1011 5. So the exponent is -1: 011 in excess 3 6. Giving us: 0 011 1011

What is 10111100 in decimal?

1. Split it apart into its pieces: 1 011 1100 2. The sign is 1 or negative 3. The exponent is 011 or -1 4. The mantissa is 1100. Move the radix one place to the left: .01100 giving us 3/8 5. Our number is -3/8

What is 01011010?

1. Split it apart into its pieces: 0 101 1010 2. The sign is 0 or positive 3. The exponent is 101 or +1 4. The mantissa is 1010. Move the radix one place to the right: 1.010 giving us 1 1/45. Our number is +1 1/4

Page 29: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

Numbering systems answer key16^3 (4096) 16^2(256) 16^1 (16) 16^0 (1) equation answ

er1 E2 "E" (14x16)= 224 "2" (2x1)=2 224+2 62 987 "9" (9x256) = 2304 "8" (8x16) = 128 "7" (7x1)=7 2304+128+7 24393 FFF "F" (15x256) = 3840 "F" (15x16) = 240 "F"

(15x1)=153840+240+15 4095

4 A4 "A" (10x16) = 160

"4" (4x1)=4 160+4 164

5 CDD3

"C" (12x4096)=49152 "D" (13x256)=3328 "D" (13x16)= 208 "3" (3x1)=3 49152+3328+208+3 52691

6 4 E 8

"4" (4x256)= 1024 "E" (14x16)= 224 "8" (8x1) = 8 1024+224+8 1256

7 AA53

"A" (10x4096)=40960 "A" (10x256)=2560 "5" (5x16)=80 "3" (3x1)=3 40960+2560+80+3 43603

8 12BB

"1" (1x4096)=4096 "2" (2x256)=512 "B" (11x16)=176 "B" (11x1)=11

4096+512+176+11 4795

9 1AFF

"1" (1x4096)=4096 "A" (10x256)=2560 "F" (15x16) = 240 "F" (15x1)=15

4096+2560+240+15 6911

Binary addition: try these problems

11__ 1__ 111_ 111_ 1_1__ 11____ 11111_ 1____1010 111 101 111 1010 11101 11111 1001

+1111 +1101 +111 +111 +1010 +1010 +11111 +100011001 1101 1000 1110 10100 100111 111110 10001

Binary subtraction: try these problems

100 101 1001 1101 10001 111 111 1011011110-10 -11 -11 -11 -100 -110 -111 -1011011110100 10 0 1010 1101 1 0 0

Binary multiplication: try these problems

10 100 101 1101 11011x10 x11 x10 x11 x1010 100 0 1101 11011

+10 +1000 +1010 +11010 010 1100 1010 100111 +1101100

10001111

Page 30: ecology labfaculty.cse.tamu.edu/slupoli/notes/CSOverview/Numbe… · Web viewbinary, you have only two options ( base 2 = 0 – 1) options usually START at 0 none of the character

Convert these to Two's Compliment then solve, all 4 bit:

7 "0111" 6 "0110" 4 "0100" 2 "0010" 1 "0001"-5 "1011" -1 "1111" -6 "1010" -4 "1100" -5 "1011"

1"0010" 1"0101" "1110" "1110" "1100"

Sources:

http://everything2.com/title/Binary%2520mathematicshttp://www.binarymath.info/practice-exercises.phphttp://www.geek.com/articles/chips/binary-rounding-algorithms-by-clive-max-maxfield-2006014/ (floating point image)http://www.lcsc.edu/hapmcneill/CS111/notes/floats.html (floating point rep)