P_Monty
Posts: 57
Joined: Sat Dec 27, 2014 2:45 pm
Location: Wiltshire, UK

Bitwise operaions.

Mon May 09, 2016 6:57 pm

Hi folks - I hope thats a suitable subject - not totally sure how to describe what I'm aiming for - but here goes...
I've got a little application I'm working on where a logic variable changes state when a button is pressed. I can do this quite simply with:
new_state=1-old_state
Now, I wanted to do this with multiple buttons and I figured having multiple variables was a bit wasteful, so I could do it all by changing the state of individual bits in a byte.

Here's the tricky part - I can read individual bits by doing a bitwise and (x&y) with 1,2,4 etc. But I can't figure out how to write the new values back into correct position of the byte. I don't think adding individual values of 1,2,4 etc will work as the maths could soon get messy...

Any ideas? (Does that even make sense?)

Thanks

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Bitwise operaions.

Mon May 09, 2016 7:24 pm

To set a bit.

bits |= (1<<GPIO)

To clear a bit.

bits &= (~(1<<GPIO))

P_Monty
Posts: 57
Joined: Sat Dec 27, 2014 2:45 pm
Location: Wiltshire, UK

Re: Bitwise operaions.

Fri May 13, 2016 10:53 am

Thanks for the reply.
Where I'm really stuck is how to change an individual bit in a byte.
Suppose I've got a byte which is currently 00000000 in binary, if I want a particular button (either GPIO or screen-press it doesn't matter) to change the byte to 00000100, I'm not sure how to do that.

In this simple example, I can just add 4 - but I think it'll be more complicated than that in a real example...

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Bitwise operaions.

Fri May 13, 2016 11:19 am

P_Monty wrote:Thanks for the reply.
Where I'm really stuck is how to change an individual bit in a byte.
Suppose I've got a byte which is currently 00000000 in binary, if I want a particular button (either GPIO or screen-press it doesn't matter) to change the byte to 00000100, I'm not sure how to do that.

In this simple example, I can just add 4 - but I think it'll be more complicated than that in a real example...
The code I posted changes an individual bit with a byte or word or whatever size you are using.

If you are happier just mentally change GPIO to BIT.

MarkTF
Posts: 318
Joined: Tue Mar 03, 2015 4:59 pm

Re: Bitwise operaions.

Fri May 13, 2016 2:15 pm

An example to expand on Joan's post:

Code: Select all

a = 0b00000000      # a = 0 (set using binary notation)
print "a set to zeros: {0:08b}".format(a)    # print a as 8 bit binary number
a = a | 0b00000100  # set bit position 2 using bitwise logical "or" operation
print "third lsb set: {0:08b}".format(a)    # print a as 8 bit binary number
a = a | 0b00001000  # set bit position 3 using bitwise logical "or" operation
print "fourth lsb set: {0:08b}".format(a)    # print a as 8 bit binary number
a = a & 0b11111011  # clear bit position 2 using bitwise logical "and" operation
print "third lsb cleared: {0:08b}".format(a)    # print a as 8 bit binary number
That said, unless one has large amounts of data it's probably more clear (hence less subject to silly mistakes), to use one variable per bit at the cost of some additional program size.

P_Monty
Posts: 57
Joined: Sat Dec 27, 2014 2:45 pm
Location: Wiltshire, UK

Re: Bitwise operaions.

Fri May 13, 2016 4:02 pm

I'm being a bit slow here...
In your example, is "bits" The name of the byte that is having the individual bits changed? And where you use a"1" does that mean I could use a "2" or "4" for other bits in the byte?

Edit:
I hadn't seen MarkTFs post when I wrote this.

I think I'm understanding now - thanks folks.

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Bitwise operaions.

Fri May 13, 2016 4:20 pm

P_Monty wrote:I'm being a bit slow here...
In your example, is "bits" The name of the byte that is having the individual bits changed? And where you use a"1" does that mean I could use a "2" or "4" for other bits in the byte?
bits is the variable which may be a byte (8 bits) or a long (32 bits) or anything you want. The 1<<GPIO syntax means shift 1 GPIO bits from the bottom of the byte. So 1<<0 identifies what is normally called bit 0, 1<<7 identifies bit 7, 1<<x identifies bit x. Have a look in a Python tutorial.

Return to “Python”