Questions? Email sfo.lax6776@gmail.com

Join our weChat group or follow us on Facebook

©2019 by Legion of Learners

sfolax6776
Jul 10, 2018

2.11 Conversion between binary and decimal numbers

3 comments

Edited: Oct 14

A. Introduction

Turn a binary number into decimal is easy: just multiply each digit with its place value and add up. e.g. 1101 = 1*2^3 + 1*2^2 + 0*2^1 + 1*2^0 = 8+4+1=13

B. Two ways to turn decimal into binary:

  1. Divide the number by 2 until the quotient is 0, then write down all the remainder backwards.

 

2. Descending power of 2 and subtract:

  1. Find the largest power of 2 that can fit in the decimal number x, let that be n. If x>2^n, note down 1 (from highest to lowest, i.e. left to right) otherwise note down 0.

  2. x = x - 2^n

  3. n=n-1 if n>=0, go back to step 1; otherwise done.

C. Try:

  • Convert the following binary numbers into decimal numbers:

1011, 1101, 1111, 10001, 11001, 1100010011, 1000111011.

  • Convert the following decimal numbers into binary numbers:

123, 789, 9021, 8762, -1000000, 0.5

mr.ricklu
Jul 14, 2018

 

 

wangernestrdragon
Jul 15, 2018

1)2^3+2^1+2^0=11

2)2^3+2^2+2^0=13

3)2^3+2^2+2^1+2^0=14

4)2^4+2^0=17

5)2^4+2^3+2^0=25

6)2^9+2^8+2^4+2^1+2^0=787

7)2^9+2^5+2^4+2^3+2^1+2^0=571

8)111101

9)1100010101

10)10001100111101

11)10001000111010

12)-11110100001001000000

13)0.1

tian
Jul 15, 2018

11, 13, 15, 17, 25, 787, 571

 

1111011, 1100010101, 10001100111101, 10001000111010, -11110100001001000000, 0.1

New Posts
  • sfolax6776
    7 days ago

    A. Introduction The third and last type of flow is all about repetitions: how to repeat some actions exact N times? or how to repeat some actions until certain status is achieved? There are actually three types of statements: for loops, while loops and do loops. B. Watch( Slides ) C. Try: Print out a multiplication table from 1 x 1 = 1 .... to 9 x 9 = 81. (Hint: you need a for loop inside a for loop)
  • sfolax6776
    Nov 1

    A. Introduction You were asked to use for-loops to print out a multiplication table, for-loops and nested for-loops are widely used for many applications. Here are some examples. B. Watch( Slides ) C. Try to print out the upper half or the lower half of a multiplication table like this:
  • sfolax6776
    Oct 26

    A. Introduction do loops are essentially same at while loops except that they will run at least once. All three types of loops can be converted into each other... Which one to choose? B. Watch( slides ) C. Try this: Write a do loop that reads integers and computes their sum. Stop when reading a zero or the same value twice in a row. For example, if the input is 1 2 3 4 4, then the sum is 14 and the loop stops.