[Tutor] binary data as string?
Alan Gauld
alan.gauld at blueyonder.co.uk
Fri Apr 16 03:49:30 EDT 2004
> When one does 'bitwise' movements over data in strings
> it is actually moving 'characterwise', correct?
No, characters are bytesize, bitwise means looking at
the individual bits. Usually this is done by taking a
byte and applying a "mask" to the byte to determine
the value.
For example applying a bitwise AND of 0x01 to a byte
will return the rightmost bit, by "leftshifting" the
mask and repeating we can see each individual bit in turn:
mask = 1
data = 42
for n in range(8):
print (data & mask) and 1 or 0,
mask = mask << 1
Will print out the bit pattern of 42...
HTH,
Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list