Preview lessons, content and tests

Computer Science & Programming solved. All in one platform.

1. To trial the platform and take tests, please take a few seconds to SIGN UP and SET UP FREE.
2. Searching for something specific? See our text overview of all tests. Includes 'real teacher' use videos.

Join 30000+ teachers and students using TTIO.

Bitwise Operators

The bitwise operators are similar to the logical operators, except that they work on a smaller scale -- binary representations of data.

Bitwise AND of 4-bit integers (below)

The following operators are available:

  • op1 & op2 -- The AND operator compares two bits and generates a result of 1 if both bits are 1; otherwise, it returns 0.
  • op1 | op2 -- The OR operator compares two bits and returns 1 if either or both of the bits are 1 and it gives 0 if both bits are 0.
  • op1op2 -- The EXCLUSIVE-OR operator compares two bits and generates a result of 1 if the bits are complementary; otherwise, it returns 0.
  • ~op1 -- The COMPLEMENT operator is used to invert all of the bits of the operand.
  • op1 >> op2 -- The SHIFT RIGHT operator moves the bits to the right, discards the far right bit, and assigns the leftmost bit a value of 0. 

www.teachyourselfpython.com