Binary Number Systems

Binary Number Systems

ENGI1006 PYTHON Summer 2021 1 Number Systems 2 OTHER NUMBER SYSTEMS • The western scientific world relies on decimal, or base 10 • as did the Ancient Egyptians, Incans, Greeks, Romans • Mayans used vegisimal, or base 20 • The Babylonians used sexagesimal, or base 60 • http://mentalfloss.com/article/31879/12-mind-blowing- number-systems-other-languages 3 BINARY NUMBER SYSTEMS • All modern digital devices rely on a simple scheme, intimately tied to transistors: • 1 - On • 0 - Off 4 BINARY NUMBER SYSTEMS • We call this base 2 number a bit • 8 bits form a byte • Use Greek prefixes, but in base 2 • Kilobyte is ~1000 Bytes, but is actually 2^10 bytes (1024 bytes) • Sometimes different prefixes are used (kibi vs kilo) 5 CONVERTING TO BINARY • We start by formalizing our understanding of decimal, or “base 10” • The digits of a number are multipliers for the powers of our base 6 CONVERTING TO BINARY • Thus, we can express a base 10 number as powers of 10 • 10,436 = 1*104 + 0*103 + 4*102 + 3*101+ 6*100 • Thus, in any other base, a number is composed of digits between 0 and (base - 1), times powers of the base 7 CONVERTING TO BINARY • So in binary • 10110 = 1*24 + 0*23 + 1*22 + 1*21 + 0*20 = 22 in decimal • Or in octal (base 8) • 75 in octal = 7*81 + 5*80 = 61 in decimal 8 CONVERTING TO BINARY • These operations are second nature to us in decimal, which can often make them confusing in other bases 9 CONVERTING TO BINARY • Converting a decimal number to a different base is a little bit harder • Luckily, there is a straightforward process (or algorithm, a term we will define later) for doing so • Given a number in decimal, if we divide repeatedly by a given base with remainder, the remainders will correspond to the digits in that base 10 CONVERTING TO BINARY • 1234 = 1*103 + 2*102 + 3*101 + 4*100 • = (1*103 + 2*102 + 3*101) + 4 • if we divide by 10 with remainder, its clear the remainder is 4, and the result is • 1*102 + 2*101 + 3*100 = (1*102 + 2*101) + 3 11 CONVERTING TO BINARY • Repeating the process until the quotient is 0 will yield me back the digits • Since my choice of base 10 was arbitrary, I can do the same for any other base, I.e. • 1234 = 1*103 + 2*102 + 3*101 + 4*100 • = Bn*2n + … + B0*20 • A slightly more mathematical explanation is here 12 CONVERTING TO BINARY • Let’s look at a few examples on the board 13 OTHER NUMBER SYSTEMS • Occasionally, we will use octal (base 8) • We sometimes use a hexadecimal (base 16) system to represent whole bytes with less data • i.e., (10101111)2 can be represented as (AF)16 14 15 BINARY ADDITION • Just like any other number system, we can do arithmetic • In fact, since all data on your computer is stored in binary, this is how the computer does calculations 16 BINARY ADDITION • Addition in decimal is second nature to us, but lets think about how we do it • To add up 2 numbers, we simple add them digit by digit • Then for any given pair of digits, since the numbers must be between 0 and 9, the resulting addition is between 0 and 18 • If the result requires more than one digit (e.g. 10 to 18), we carry over the second digit to our next calculation 17 BINARY ADDITION • In binary, it works the same. Each pair of numbers is between 0 and 1, so the resulting addition is between 0 and 3 in decimal, or between 0 and 11 in binary (0, 1, 10, or 11) • If the result requires more than one digit, we carry over the second digit to our next calculation 18 BINARY ADDITION • Let’s look at a few examples on the board 19 DEALING WITH NEGATIVES • In the decimal system, we use a “-“ to delineate negative numbers • In the binary system, there are two primary options • using a single bit for sign • two’s complement, a special scheme 20 DEALING WITH NEGATIVES • Signed binary numbers utilize the first bit as just an indicator of the sign of the remaining bits • So in 4 bits, the number 6 is represented as 0110 or equivalently +110, while the number -6 is represented as 1110 or equivalently -110 21 DEALING WITH NEGATIVES • There are 2 big drawbacks to this scheme • First, we always have both +0 and -0, which is annoying if we ever want to make comparisons to 0 • For example, if we want to check if a number is =0, we must check if = +0 or = -0 22 DEALING WITH NEGATIVES • Additionally, to do operations like (5 - 7), we need to do the same complicated subtraction algorithms we learned as children, but with the added complexity of binary • Wouldn’t it be great if we could just use the simple addition we already know? 23 COMPLEMENT • We can utilize the method of complements to make subtraction easy • You might have learned this for decimal as a way to do quick mental subtraction • This method makes binary subtraction a lot easier, with the caveat that we must only operate on a fixed number of bits (e.g. we limit ourselves to 4 bits) 24 ONE’S COMPLEMENT • One’s complement is not widely used, but its worth it as a stepping stone to two’s complement • For positive numbers, we represent them the same as signed binary • For negative numbers, we represent them by taking the positive number and flipping all the bits • Thus, all positive numbers start with 0 and all negative numbers start with 1 25 ONE’S COMPLEMENT • The number 6 is represented as 0110 • The number -6 is represented as 1001 • Given an unknown number 1010, we know its negative because it starts with 1, so we flip the bits • The result is 0101 = 5, so 1010 = -5 26 ONE’S COMPLEMENT 011 3 3 010 2 2 001 1 1 000 0 0 111 -3 -0 110 -2 -1 101 -1 -2 100 -0 -3 27 ONE’S COMPLEMENT • To do subtraction then becomes easy • To do 4-7, we just do 4+(-7) • 4 is 0100, 7 is 1000 • 0100 + 1000 = 1100 = -(0011) = -3 28 ONE’S COMPLEMENT • We’ve solved our subtraction problem, but unfortunately • +0 = 0000 = 1111 = -0 • so we still have 2 zeros 29 ONE’S COMPLEMENT • To fix this, we should shift our negative numbers down by one • So in 4 bits, instead of our numbers going between 0 and 7 and -0 and -7, they’ll go between 0 and 7 and -1 and -8, with no duplicates • This scheme is called Two’s Complement 30 TWO’S COMPLEMENT • Similarly to one’s complement and signed binary, all positive numbers start with 0 and all negative numbers start with 1 • The only difference with one’s complement is that after flipping the bits, we add 00…..01 to our number • Its counterintuitive to add 1 when we want to subtract one from our negative numbers, but remember that one’s and two’s complement make negative numbers symmetric 31 TWO’S COMPLEMENT • The number 6 is represented as 0110 • The number -6 is represented as 1010 • For -6, we start with 6 (0110), flip the bits (1001), and then add 0001 (1010) • Given an unknown number 1010, we know its negative because it starts with 1, so we flip the bits and add 1 • 1010, then flip the bits (0101), then add 0001 (0110), so 1010 = -6 32 TWO’S COMPLEMENT 011 3 3 010 2 2 001 1 1 000 0 0 111 -3 -1 110 -2 -2 101 -1 -3 100 -0 -4 33 TWO’S COMPLEMENT • Subtraction remains easy • To do 4-7, we just do 4+(-7) • 4 is 0100 • To figure out -7 we start with 7 (0111), flip the bits (1000) and add 0001 ( 1001) • 0100 + 1001 = 1101 • We know 1101 is negative because it starts with a 1, so we flip the bits (0010) and add 0001 (0011) • so our result is -3 34 TWO’S COMPLEMENT • Let’s go through a few conversion and addition examples on the board 35.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    35 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us