Re: how to get bit info

2010-06-19 Thread Anssi Saari
Back9 writes: > Hi, > > I have one byte data and want to know each bit info, > I mean how I can know each bit is set or not? Other than the tedious anding, oring and shifting, you can convert your byte to a string (with function bin) and use normal string handling functions to check if individua

Re: how to get bit info

2010-06-17 Thread Stephen Hansen
On 6/17/10 1:29 PM, Grant Edwards wrote: > On 2010-06-17, Stephen Hansen wrote: > > BIT_1 = 1 << 0 > BIT_2 = 1 << 1 > > ... > >> Basically, those BIT_X lines are creating numbers which have *only* the >> specified bit set. Then you do "byte & BIT_X", and that will return 0 if >> the byt

Re: how to get bit info

2010-06-17 Thread Grant Edwards
On 2010-06-17, Stephen Hansen wrote: BIT_1 = 1 << 0 BIT_2 = 1 << 1 ... > Basically, those BIT_X lines are creating numbers which have *only* the > specified bit set. Then you do "byte & BIT_X", and that will return 0 if > the byte doesn't have the specified bit in it. You can then set

Re: how to get bit info

2010-06-17 Thread Irmen de Jong
On 17-6-2010 21:51, Back9 wrote: Hi, I have one byte data and want to know each bit info, I mean how I can know each bit is set or not? TIA Use bitwise and, for instance, to see if the third bit is set: byte = 0b if byte & 0b0100: print "bit is set" -irmen -- http://mail

Re: how to get bit info

2010-06-17 Thread Stephen Hansen
On 6/17/10 12:51 PM, Back9 wrote: > I have one byte data and want to know each bit info, > I mean how I can know each bit is set or not? >>> BIT_1 = 1 << 0 >>> BIT_2 = 1 << 1 >>> BIT_3 = 1 << 2 >>> BIT_4 = 1 << 3 >>> BIT_5 = 1 << 4 >>> BIT_6 = 1 << 5 >>> BIT_7 = 1 << 6 >>> BIT_8 = 1 << 7 >>> byte

Re: how to get bit info

2010-06-17 Thread Tim Lesher
On Jun 17, 3:51 pm, Back9 wrote: > Hi, > > I have one byte data and want to know each bit info, > I mean how I can know each bit is set or not? You want the bitwise-and operator, &. For example, to check the least significant bit, bitwise-and with 1: >>> 3 & 1 1 >>> 2 & 1 0 -- http://mail.pyth

Re: how to get bit info

2010-06-17 Thread Laurent Verweijen
Op donderdag 17-06-2010 om 12:51 uur [tijdzone -0700], schreef Back9: > Hi, > > I have one byte data and want to know each bit info, > I mean how I can know each bit is set or not? > > TIA def bitset(x, n): """Return whether nth bit of x was set""" return bool(x & (1 << n)) -- http://mail.pyt