Manipulating specific bits

Ken Seehof kens at sightreader.com
Wed Jun 21 09:28:43 EDT 2000


>>> byte = 0
>>> byte = byte | (1<<2)|(1<<5) # 2nd and 5th bit starting at the 0th bit
>>> byte
36

"|" = or operator
"&" = and operator
"<<" = left shift
">>" = right shift
"0x13" = hexadecimal representation of 19

Stephen Hansen wrote:

>     Okay, this is a Very Newbie-ish question.
>
>     What I need to do is manipulate specific bits in a byte, for
> transmission. I have absolutely *no* idea how to do this. :)
>     For example, say I have my 1 byte: 0 0 0 0 0 0 0 0. Say I want to set
> the 2nd and 5th bit to 1.. how the heck do I do that? :)
>     I've never actually even /blinked/ in the direction of bit-ish
> manipulation, so am utterly clueless. Next i'm gonna have to stick it into a
> struct I think, before transmission.. I think. dunno. Will look into that
> afterwards.
>
> Thanks. :)
>
> --S

--
Ken Seehof
kens at sightreader.com
starship.python.net/crew/seehof
Hi! I'm a .signature virus! copy me into your .signature file to help me spread!


-------------- next part --------------
A non-text attachment was scrubbed...
Name: kens.vcf
Type: text/x-vcard
Size: 343 bytes
Desc: Card for Ken Seehof
URL: <http://mail.python.org/pipermail/python-list/attachments/20000621/5bd65a2e/attachment.vcf>


More information about the Python-list mailing list