Would somebody explain me? :(


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>

<What do you expect to happen instead?>

```python

When we count, we usually do it in base 10. That means that each place in a number can hold one of ten values, 0-9. In binary we count in base two, where each place can hold one of two values: 0 or 1. The counting pattern is the same as in base 10 except when you carry over to a new column, you have to carry over every time a place goes higher than one (as opposed to higher than 9 in base 10).

For example, the numbers one and zero are the same in base 10 and base 2. But in base 2, once you get to the number 2 you have to carry over the one, resulting in the representation “10”. Adding one again results in “11” (3) and adding one again results in “100” (4).

Contrary to counting in base 10, where each decimal place represents a power of 10, each place in a binary number represents a power of two (or a bit). The rightmost bit is the 1’s bit (two to the zero power), the next bit is the 2’s bit (two to the first), then 4, 8, 16, 32, and so on.

The binary number ‘1010’ is 10 in base 2 because the 8’s bit and the 2’s bit are “on”:

8’s bit 4’s bit 2’s bit 1’s bit
1 0 1 0
8 + 0 + 2 + 0 = 10
In Python, you can write numbers in binary format by starting the number with 0b. When doing so, the numbers can be operated on like any other number!

Excersise:
print 0b1, #1
print 0b10, #2
print 0b11, #3
print 0b100, #4
print 0b101, #5
print 0b110, #6
print 0b111 #7
print "******"
print 0b1 + 0b11
print 0b11 * 0b11

I DON´T GET THE EXPLANATION

<do not remove the three backticks above>

#2

here:

8   4   2   1 bits
1   0   1   0 signal

we have 8 bits, 4 bits, 2 bits and 1 bit.

computers know two things, on and off (1 or 0):

below the 8 bits there is 1 (on), which means we have 8 bits
below the 4 bits there is 0 (off), so we don’t have 4 bits
below the 2 bits there is 1 (on), so we have 2 bits + 8 bits is 10 bits
below the 1 bits there is 0 (off), so we don’t have 1 bit

so total we now have 10 bits


#3

sorry but the explaination isn´t very clear


#4

combined with the exercise information? That is tricky. Just saying you don’t understand, i can’t improve my answer

show me what you do understand, so i can fill in the gaps, which mean i can custom tailor my answer to what you are missing

otherwise, you might just find as well a guide online and read it. Use the strength of the forum, something guides can’t do


#5

Still do not get it.


#6

#7

#9

Stack them up…

0b 0001
0b 0011
      0 carry 1

0b 0010 carry
0b 0001
0b 0011
      0
     0  carry 1

0b 0100 carry
0b 0001
0b 0011
      0
     0
    1
--------
0b 0100

1 plus 3 is most certainly 4. Just as 5 plus 5 is 10, (each is half of ten), 0b1 plus 0b1 is 2 (each is half of two).

Let’s try 3 times 3. Computationally, binary numbers are rather cumbersome to multiply, but we can always do it the way the computer does, by adding.

0b 0011    (3)
0b 0011    (3)
-------
0b 0110    (6)
0b 0011    (3)
-------
0b 1001    (9)

Be more specific with your question so we get an idea of where you having trouble.


#10

i don´t know how to represent numbers in binary code


#11

Numbers in ‘binary code’ are all 1’s and 0’s. In other words, binary code is ALL DATA that must be parsed and compiled according to the machine language rules. Machine code looks like any other binary data.

The most common number systems used in computing are base 2, base 8, and base 16 (binary, octal and hexadecimal, respectively).

0b 1111 1111
 0       377
0x        FF

All of the above are representations of the base 10 equivalent value, 255.

Of course I can see that your question relates to representing numbers, not code, so forgive my glibness. I still do not quite understand your question or where the difficulty arises in your particular case. Be more specific.


#12

if you say to me, represent the number 456 on binary code, i don´t know how to do it.

please teach me :slight_smile: that´s all

thanks


#13

How well do you know your powers of 2?

Eg.

0   1   2   3   4 ... exponents  2 ** x

1   2   4   8  16 ... powers

#14

i know them very well


#15

Create a table up to 2 to the 10th

 1    2    4    8   16   32   64  128  256  512 1024

Now reverse the table…

1024 512  256  128   64   32   16    8    4    2    1

Now find the numbers in the table that 456 is between…

512 > 456 > 256

Choose the value that is LESS than 456, eg. 256. Under that number write a 1

1024 512  256  128   64   32   16    8    4    2    1
            1    1    1    0    0    1    0    0    0

Subtract 256 from 456 => 456 - 256 == 200. We see that the numbers this is between are 256 and 128, so choose 128 (since it is less than 200) and write a 1 in the table above.

Subtract 128 from 200, which gives 72. That number is between 128 and 64 so choose 64 and write a 1 under it in the table.

Subtract 64 from 72, which gives 8. Under the 32 and under the 16 write a zero since those bits are not set. Then write a 1 under the 8.

8 minus 8 is zero, so the remaining bits will be 0. Now combine all the bits into a single integer…

 0b 1 1100 1000

so 456 (decimal) == 0b111001000 (binary). That is how to convert decimal to binary on paper.

Practice it with a range of numbers up to 1024. Pick a dozen at random and convert each one. When you have completed that exercise (it is important to get this practice to be automatic in one’s mind, so do the practice, on paper) check all your answers in the interactive console or a repl.it sandbox.

>>> print (bin(456))
0b111001000
>>> print ('0b111001000' == bin(456))
True
>>> 

Note the quotes since bin() returns a string.


#16

Challenge… How would we write the above algorithm (series of steps) in program terms (Python script)?

  1. Write a function that returns the highest power of two in a number, and another function to find the exponent.
>>> from math import log10

>>> def highest_power_of_2(n):
    return 2 ** int(log10(n) / log10(2))

>>> highest_power_of_2(456)
256
>>> def log_n_base_2(n):
	return int(log10(n) / log10(2))

>>> log_n_base_2(highest_power_of_2(456))
8
>>> 

If you haven’t studied exponents and logarithms, then the above will look like Greek, but accept that it works and study up on how and why.

That will be our engine, moving forward. The rest involves looping over our number subtractions.

  1. Write a function that loops over a number according to our above subtraction algorithm:
>>> def dec_to_bin_converter(n):
    b = ['0'] * (log_n_base_2(n) + 1)
    while n > 0:
        m = highest_power_of_2(n)
        n -= m
        b[log_n_base_2(m)] = '1'
    return '0b' + ''.join(b[::-1])

>>> dec_to_bin_converter(456)
'0b111001000'
>>> dec_to_bin_converter(456) == bin(456)
True
>>> 

Study this code example closely and try to figure how it works. How does it compare to our paper version?

At this point in time we are not seeking efficiency or elegance. Our aim here was to replicate the paper algorithm.


Our engine above can also be written using natural logarithms…

>>> from math import log
>>> def highest_power_of_2(n):
    return 2 ** int(log(n) / log(2))

>>> highest_power_of_2(456)
256
>>> def log_n_base_2(n):
    return int(log(n) / log(2))

>>> log_n_base_2(256)
8
>>>

You may also find this topic to be helpful…

Binary, Hexadecimals, Etc


#17

A more visual representation of whats being talked about


#18