Bitwise operators

Board Discussion Number 1

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 8812 Views - Last Post: 16 December 2010 - 11:57 AM Rate Topic: -----

#1 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 837
  • View blog
  • Posts: 2,271
  • Joined: 23-December 08

Bitwise operators

Post icon  Posted 13 December 2010 - 11:45 AM

Hey everybody! In an attempt to bring some more life to the Python board, I plan on trying to start discussions on a fairly regular basis. Today's topic: Bitwise Operators

I don't use bitwise operators much myself. A buddy of mine often uses them when he knows he's going to be multiplying by 2. He also showed me a clever algorithm for isolating values in a hexadecimal rgb value; that one REALLY impressed me.

So my question to you guys is this: Do you ever use bitwise operators? If so, when? I bet some of you have some handy uses for them in regards to boolean flags. I am really interested in hearing some board feedback! :walkman:

This post has been edited by Dogstopper: 13 December 2010 - 01:16 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Bitwise operators

#2 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Bitwise operators

Posted 13 December 2010 - 01:13 PM

Topic Description edited to make it more clear and Topic Pinned & Featured. Great idea atraub!

This post has been edited by Dogstopper: 13 December 2010 - 01:16 PM

Was This Post Helpful? 0
  • +
  • -

#3 dorknexus   User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1272
  • View blog
  • Posts: 4,625
  • Joined: 02-May 04

Re: Bitwise operators

Posted 13 December 2010 - 01:23 PM

In the case of multiplying or dividing by 2, you should not include this in your code directly. In my opinion it makes the code less readable and in reality a decent optimizing compiler will convert divisions by two and what not into bit shifts anyhow and your code remains readable.
Was This Post Helpful? 0
  • +
  • -

#4 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 837
  • View blog
  • Posts: 2,271
  • Joined: 23-December 08

Re: Bitwise operators

Posted 13 December 2010 - 01:26 PM

That's a fair point, I don't really know how much optimizing Python's interpreter does. I suppose that a good counter argument would be that a simple comment restores readability.
Was This Post Helpful? 0
  • +
  • -

#5 Seta00   User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 84
  • Joined: 22-September 10

Re: Bitwise operators

Posted 13 December 2010 - 01:26 PM

View Postatraub, on 13 December 2010 - 02:45 PM, said:

So my question to you guys is this: Do you ever use bitwise operators? If so, when? I bet some of you have some handy uses for them in regards to boolean flags. I am really interested in hearing some board feedback! :walkman:


Yes, a lot, when I'm coding critical speed applications, but no, I don't care about them when I'm using Python :P
Was This Post Helpful? 0
  • +
  • -

#6 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 837
  • View blog
  • Posts: 2,271
  • Joined: 23-December 08

Re: Bitwise operators

Posted 13 December 2010 - 01:28 PM

Awww come on Seta00, can't you expand a bit more on your response than just "yep, I sure do use em when it counts." DISCUSS MAN!
Was This Post Helpful? 0
  • +
  • -

#7 Seta00   User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 84
  • Joined: 22-September 10

Re: Bitwise operators

Posted 13 December 2010 - 01:52 PM

View Postatraub, on 13 December 2010 - 04:28 PM, said:

Awww come on Seta00, can't you expand a bit more on your response than just "yep, I sure do use em when it counts." DISCUSS MAN!


Well, "when it counts" is all that matters for that case :P

I use Python because of its readability, flexibility, incredible speed of development, and the HUGE amount of code available out there, so the benefits of bitwise operators, even though they can be enormous on other applications, don't make any difference for me.
Was This Post Helpful? 0
  • +
  • -

#8 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 837
  • View blog
  • Posts: 2,271
  • Joined: 23-December 08

Re: Bitwise operators

Posted 13 December 2010 - 02:00 PM

Bah! Regardless of any of that, you can still explain how you use bitwise operators in the contexts of other languages. Python does have Turing compatibility, so just about anything you can use bitwise operators for in those languages can be applied to Python too!

You're like a bad date who only gives one word answers :P
Was This Post Helpful? 0
  • +
  • -

#9 moopet   User is offline

  • binary decision maker
  • member icon

Reputation: 345
  • View blog
  • Posts: 1,190
  • Joined: 02-April 09

Re: Bitwise operators

Posted 13 December 2010 - 02:01 PM

Used to use bitmasking a lot in C. Shifting indexes made enumerations or a series of #defines a lot more readable:
#define XX (1 << 2)
#define YY (1 << 3)
// etc


I can't off the top of my head think of any situations where I'm likely to use them in Python. Comparing flag values in bit-type database fields from data sources that don't support it natively? Any situation I can think of, I can also think of a library that wraps it for me in anyway :)
Was This Post Helpful? 0
  • +
  • -

#10 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 837
  • View blog
  • Posts: 2,271
  • Joined: 23-December 08

Re: Bitwise operators

Posted 13 December 2010 - 02:07 PM

I know it's not Python, but I still love it:

#define XX (1 << 2)
#define YY (1 << 3)
// etc


That's a clever use for em! Thank you moopet!
Was This Post Helpful? 0
  • +
  • -

#11 Seta00   User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 84
  • Joined: 22-September 10

Re: Bitwise operators

Posted 13 December 2010 - 02:12 PM

View Postatraub, on 13 December 2010 - 05:00 PM, said:

Bah! Regardless of any of that, you can still explain how you use bitwise operators in the contexts of other languages. Python does have Turing compatibility, so just about anything you can use bitwise operators for in those languages can be applied to Python too!

You're like a bad date who only gives one word answers :P


Oh, so you want examples and all, I thought this was just about Bitwise Operators in Python.

Well, I use Pawn/SourcePawn to mod my Counter-Strike and Team Fortress 2 servers on my spare time, and Pawn has a nice feature: you can specify the increment operation of an enum. So if I want an enumeration of, let's say player specific "thread" ids, I can do:

enum (+= 32) {
    Task_CheckPlayerHealth,
    Task_CheckPlayerAmmo,
    // and so on
}


They come in handy when you want to use bitmasks, so only what you need to do is:

enum (<<= 1) {
    Flag_InWater = 1, // just remember to set the first one to 1
    Flag_OnGround,
    Flag_InAttack,
    Flag_InAttack2,
    // etc
}



I also have a set of macros for player bitfields easy creation/manipulation.
They are very effective, mainly because the Pawn compiler can't optimize shit :P
(But that's also a good thing, since I like wandering through my code trying to find places where I can take it to max speed)

This post has been edited by Seta00: 13 December 2010 - 02:14 PM

Was This Post Helpful? 0
  • +
  • -

#12 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 837
  • View blog
  • Posts: 2,271
  • Joined: 23-December 08

Re: Bitwise operators

Posted 13 December 2010 - 02:50 PM

Thanks Seta00. Your post actually may be more relevant than you realize. Python is establishing its' place in the scripting and modding community. I know that Python is HEAVILY used by the folks hard at work modding Star Trek Bridge Commander. Here's a list from Python.org of games that use Python as a scripting language.

Python used in game scripting

--UPDATE--
I didn't even notice that EVE online is on that list. That's awesome :walkman:

This post has been edited by atraub: 13 December 2010 - 02:52 PM

Was This Post Helpful? 0
  • +
  • -

#13 Seta00   User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 84
  • Joined: 22-September 10

Re: Bitwise operators

Posted 13 December 2010 - 03:02 PM

If only CPython wasn't a total mess to embed...
Was This Post Helpful? 0
  • +
  • -

#14 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 837
  • View blog
  • Posts: 2,271
  • Joined: 23-December 08

Re: Bitwise operators

Posted 13 December 2010 - 03:08 PM

Haha maybe that'll be another discussion Seta. I hope we get some more input! This is a topic I've been really interested in for a while!
Was This Post Helpful? 0
  • +
  • -

#15 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Bitwise operators

Posted 14 December 2010 - 06:34 AM

One reason for bit manip is flags:
# A collection of flags
(ACCESS_NONE, ACCESS_BROWSE, ACCESS_READ, ACCESS_WRITE) = (0,1,2,4)

access = ACCESS_BROWSE | ACCESS_READ
if not (access & ACCESS_READ):
	print "Access denied"



Another might be composite numbers:
def getRgb(r,g,B)/>: return (r<<16) + (g<<8) + b
def getG(rgb): return (rgb>>8) & 0xff



However, Python has usually has more elegant ways to deal with these things. Any gains from such games in an interpreted environment are questionable. So bit twiddling is more has value for interop more than anything else.

I would do this far more in C. If I saw it in Python, I'd look for an external library or a frustrated C programmer. :P
Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2