What are bitwise operators good for?

A forum for general discussion of the Python programming language.

What are bitwise operators good for?

Postby XndrK » Sun Sep 15, 2013 8:53 pm

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

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 169
Joined: Sat Jun 15, 2013 7:57 pm

Re: What are bitwise operators good for?

Postby metulburr » Sun Sep 15, 2013 11:25 pm

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
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1299
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: What are bitwise operators good for?

Postby XndrK » Mon Sep 16, 2013 4:24 pm

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 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 169
Joined: Sat Jun 15, 2013 7:57 pm

Re: What are bitwise operators good for?

Postby micseydel » Mon Sep 16, 2013 6:20 pm

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.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1113
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: What are bitwise operators good for?

Postby XndrK » Tue Sep 17, 2013 4:30 pm

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

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 169
Joined: Sat Jun 15, 2013 7:57 pm


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 1 guest