## What are bitwise operators good for?

A forum for general discussion of the Python programming language.

### What are bitwise operators good for?

Seriously.
Proverbs 26:14 describes me a bit too well.

Version: Python 3.4.3

https://github.com/4of92000
https://github.com/PythonForum/

XndrK

Posts: 208
Joined: Sat Jun 15, 2013 7:57 pm

### Re: What are bitwise operators good for?

bit manipulation, what else?

They work on the binary bits of numbers. Take 56 which is 00110001 in binary, and 235 which is 11101011 in binary.

First, you have bitwise AND(&). This compares each bit. If the bits from both of the numbers are 1, then the resulting bit is 1. Otherwise, the resulting bit is 0. For example:

56 00111000
235 11101011
&-------
40 00101000

Next, you have bitwise OR(|). If either bit is 1, then the resulting bit is 1.

56 00111000
235 11101011
|-------
251 11111011

Also, you have bitwise XOR(^). If only one of the bits is 1, then the resulting bit is 1 (if both are 1, or neither are 1, then the resulting bit is 0).

56 00111000
235 11101011
^-------
211 11010011

The final bitwise operator is bitwise NOT (~). This works on only one operand - it simply flips the bits.

56 00111000
~-------
199 11000111

235 11101011
~-------
20 00010100
we will be moving to python-forum.io on October 1 2016
more details here

metulburr

Posts: 2244
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

### Re: What are bitwise operators good for?

I knew you could use it to mess with bits, and I guess you could use it to make a Turing machine, but are they good for anything that's not directly related to bits? (I say directly for obvious reasons.)
Proverbs 26:14 describes me a bit too well.

Version: Python 3.4.3

https://github.com/4of92000
https://github.com/PythonForum/

XndrK

Posts: 208
Joined: Sat Jun 15, 2013 7:57 pm

### Re: What are bitwise operators good for?

Sometimes bit operations can be done much, much more efficiently than higher level solutions. Take this problem that I can't remember the source of

Someone wrote:You are given a stream of N unsorted integers 0 to N-2 inclusive that you do not have random access to or the ability to modify in-place. One of the numbers appears 2 times, and the rest a single time. Your goal is to find the duplicate number. There is a O(n) time and O(1) space solution to this problem.

There is a bit operation that accomplishes this with an efficiency unparalleled by any complex data structure. Such solutions rarely come up, but are AWESOME when they do; the solution to this is extremely simple and short as well. Bit operations are just another tool in a programmer's tool box for solving problems.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

### Re: What are bitwise operators good for?

Okay. Thanks.
Proverbs 26:14 describes me a bit too well.

Version: Python 3.4.3

https://github.com/4of92000
https://github.com/PythonForum/

XndrK

Posts: 208
Joined: Sat Jun 15, 2013 7:57 pm