how to get bit info

B

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
 
T

Tim Lesher

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:
0
 
L

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))
 
I

Irmen de Jong

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 = 0b11111111
if byte & 0b00000100:
print "bit is set"

-irmen
 
S

Stephen Hansen

I have one byte data and want to know each bit info,
I mean how I can know each bit is set or not?
... print "Bit 1 is set!"
... else:
... print "Bit 1 is not set!"
Bit 1 is set!... byte = byte | BIT_6
... print "Bit 6 wasn't set, BUT NOW IS."
Bit 6 wasn't set, BUT NOW IS.99

(I added 'how to set a specific bit' just cuz)

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 the bit
with "byte | BIT_X", and unset the bit with "byte ^ BIT_X".

--

Stephen Hansen
... Also: Ixokai
... Mail: me+list/python (AT) ixokai (DOT) io
... Blog: http://meh.ixokai.io/


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)

iQEcBAEBAgAGBQJMGoINAAoJEKcbwptVWx/lMNAH/3B30j2W6s5K9D++2otmaob5
EP2L8AX59qRX40nClxkZdrxI9na8/N6K0GNz/kAWQHL4M5jUe+2DHa4ZxCT6U+Rd
Y/u5kB2rhWwsMD5rlLVirPiZDAcqH2JGbtAftU57ycl2RBM1gMySkyAkIlQZWIeo
Vsuh0KRsggRNZxQq+ocoxTcFt1sfG9tCUkRjkx2CyhZGVMi3OXMjlbecOPsC3AVy
T5BE8qPg3Wxw3Hg6/M9x1cGkbeccDgP3A2kapTp7lGEACx+EbniD77Iz1LFgtmoC
n2UCfF3dOR00Wskexa5BSN9HvPJnNCCn1OShLg9xlr1/YW7rdn+UBHISdF4TmIo=
=6tdY
-----END PGP SIGNATURE-----
 
G

Grant Edwards

....

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 the bit
with "byte | BIT_X", and unset the bit with "byte ^ BIT_X".

Just to clarify, "byte ^ BIT_X" inverts (toggles) bit X.

If you want to make sure bit X is a 0 (which is what people usually
mean by "unset"), you do "byte & ~BIT_X"
 
S

Stephen Hansen

Just to clarify, "byte ^ BIT_X" inverts (toggles) bit X.

If you want to make sure bit X is a 0 (which is what people usually
mean by "unset"), you do "byte & ~BIT_X"

Doh, you're correct. I got so used to the pattern of only ever flipping
the bit off after for some reason I knew it was on, like:

if blah & CONSTANT_A:
do-stuff
blah = blah ^ CONSTANT_A

That I forgot ^ was invert >_>

Ahem! Thanks for the correction.

--

Stephen Hansen
... Also: Ixokai
... Mail: me+list/python (AT) ixokai (DOT) io
... Blog: http://meh.ixokai.io/


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)

iQEcBAEBAgAGBQJMGpYHAAoJEKcbwptVWx/llIAH/iB1kaMxo9wlgYiq9lGtyJep
gff1s4qvb853ERsrPDIQPGcO/6VbeYBhbFfgvRfTMac4p0KVG4IYMuiN100QNlUq
D4rnW83u3nWsBILhkMFkxvZO48whkUAOjTU1zn8pn1Sxahbk3xKR8wF8km3dAmEV
kolNlfgrGmrPbDtBrE8Nh7YEIei70nm+b2NAQmH5Fn4+OEkP7BbiEl2Y7sKu63Dl
PUWou/RcKZS2WpeQP45LJqDLeDs0vWcaD5kkpM1DCCM6VPeEn+2EyPJ7zGSaxNZS
w65MaUWyzsct3CGJSGXlQNH/X1KQzgbRbZ+yk+sictC1OtKAqp6P/w7H7fWmv4Y=
=0da3
-----END PGP SIGNATURE-----
 
A

Anssi Saari

Back9 said:
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 individual bits are 0 or 1.

String format is also handy if you happen to need to do things like
reversing the bit order or slicing and joining together various bits
from different bytes.

I seem to invariably need to do some bit order reversing when I mess
around with serial data, which is one reason why I like Python.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,474
Latest member
AntoniaDea

Latest Threads

Top