Slide to the Left! Slide to the Right!


#1



https://www.codecademy.com/courses/python-intermediate-en-KE1UJ/1/1?curriculum_id=4f89dab3d788890003000096


please can anyone help me solve this please am stuck here


Replace this line with your code.


#2

Please show us what you have so far and we can take a look at it. Thanks.


#3

thats my code 0b1100 >> 2 == 0b1 << 2


#4

will be False.

0b1100 >> 2 == 3 (0b11)
0b1 << 2    == 4 (0b100)

#5

i cannot really understand you please can you really explain it to me


#6

The binary number system is a base 2 representation of numbers. Like in decimal (base 10) when we add a zero to a number, say 100, it becomes another magnitude of 10, 1000, and when we add a zero to a binary, say 10, it becomes another magnitude of 2, 100.

10  => binary for 2, decimal
100 => binary for 4, decimal.

Base 10 adds a digit for each magnitude of 10, base 2 adds another bit for each magnitude of 2.

Let's examine how the magnitudes stack up. Consider and 8-bit number. Above the bits I will write the power of 2 that applies to that bit.

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

Each bit represents 2 to the corresponding power

2 **  7    6    5    4    3   2   1   0
     128 + 64 + 32 + 16 + 8 + 4 + 2 + 1  => 255

Following so far?

Notice that in the above expression, from right to left, each value is double the one before it? The effect of shifting one bit to the left has that effect. Shifting two bits to the left doubles it twice.

0b100 << 2  =>  0b10000  (add two zeros)

The value goes from 4 to 8 to 16.

Shifting to the right, halves the number with each shift.

0b10000 >> 2  => 0b100  (remove two zeros)

The value goes from 16 to 8 to 4.

Let's look at another example with two bits set.

0b1010  => 10 decimal

0b1010 >> 1  => 0b101  => 5 decimal

0b101 >> 1  => 0b10  => 2 decimal

But 2 is not half of 5? No matter, in binary there are no decimal fractions (floats). Binary numbers are integers.. Bits that get shifted off disappear unless we cache them (for another discussion).

>>> 5 >> 1
2
>>>

#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.