Bitwise operators (python)


#1

The moderators did a great job explaining the range function to me, so I am looking for similar help with bitwise operators in python. I am completely lost, can someone please leave links like they did on my last post and/or explain it in their reply?


#2

You really ought to be consulting google/equivalent
Wikipedia is generally a good place to start if they have an article on the subject
https://en.wikipedia.org/wiki/Bitwise_operation

There are also likely a lot of youtube videos, including some of high quality, discussing these operations, because they’re important (like addition and subtraction)


#3

I made a thread earlier on general bitwise operators, but the reply did not help. I found that my trouble is initial, meaning that I just don’t understand how to count binary in python, or if it is in every coding language. I think this is what I’m not getting; rather than the operators, just counting and using binary.


#4

Please post the following…

  1. A link to the exercise you are on.
  2. A code sample of your work so far
  3. Error messages

Thanks.


#5

I can’t work because I’m not understanding it. I can’t make an attempt because I don’t know what I’m doing. I think I’m on the bin() excersice under bitwise operators, but I don’t want to go any further until I am explained how to use binary.


#6

Is it lesson 4?

https://www.codecademy.com/courses/learn-python/lessons/introduction-to-bitwise-operators/exercises/the-bin-function?action=lesson_resume

Go ahead and use print and bin() to print out the binary representations of the numbers 2 through 5, each on its own line


#7

I am currently on lesson 4, yes, but that’s not my issue. My problem is that I really don’t understand lesson 2 and 3. So I need someone to help explain how to count in and use binary for bitwise operators.


#8

Bitwise operators can be used on any 32 bit signed number. That puts the numbers in the range, -2,147,483,648 .. 2,147,483,647. The inputs do not have to be binary, since the operators can coerce binaries from any number base, so long as the base is known.

In other words,

15 & 14 => 14

Python converts 15 to 0b1111 and 14 to 0b1110. The 0b tells Python that the number is a binary integer. See how 14 does not have bit 1 set?

When adding binary numbers, we start on the right just as we would with decimal numbers (base 10).

  1
+ 1
----
  0 with a carry, so
 10

  1010    =>  10 in base 10
+ 1100    =>  12 in base 10
     0    => with no carry
    10    => with no carry
   110    => with no carry
  0110    => with a carry bit, so
 10110    =>  22 in base 10

If you are familiar with exponents then it we can visualize each bit from the right as a power of 2, starting with 2 to the zero (which is always 1).

2^4 + 2^3 + 2^2 + 2^1 + 2^0   when set
 16 +  0  +  4  +  2  +  0    => 22

#9

I think the only thing you need to realise that it’s no different from the 10-digit system you’re used to. Except it’s two digits.

Multiply by 10 by adding a 0 on the right
Multiply by 2 by adding a 0 on the right

1s 10s 100s 1000s
1s 2s 4s 8s 16s

99 -> 100 -> 101
11 -> 100 -> 101


#10

so all I have to do to count in binary is multiply like that? The first explanation was too complicated, but this seems pretty simple. Also, is 1, 2, 4, 8, and 16 the only numbers you use besides 1 and 0 in binary?


#11

No, counting is done same way as ever. Add 1 to the right-most digit, carry the 1 if there’s no next numeral to use.

You can write a program that prints numbers counting up from 0, printing out the next one each time you press enter and just watch what happens

Also consider base1

1
11
111
1111
11111
111111

totally broken though, there’s no 0, or, alternatively, only 0


#12

There are no other numbers in binary but 1 and 0. There are no signs, either. Signed numbers use the highest order bit (farthest to the left) to indicate sign, 1 for negative, 0 for positive.

1, 2, 4, 8, 16, 32, 64, … are the respective values for each bit in the binary number, from right to left. The early example only shows up to 16 because the number represented the value 22, which is the sum of a set bit values.

Placement of set bits in a binary is crucial to understand because it is by the addition of their respective value that they get their integer value.

Recall from exponents that we have a base raised to the power of an exponent. The exponent is the degree of the equation and its order of magnitude is the approximate size of the base raised that degree.

y = x**10

is a degree 10 equation. In order of magnitude the number, y will have 11 digits when x is 10. Now let’s take this over to a constant base of 2.

y = 2**10

How many bits do you expect y to have?


#13

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