473,594 Members | 2,678 Online

# bit operations with python?

I am going through the Geek Challenges on the Open Source Institute
Website: http://www.osix.net/modules/geek/

The instructions for Level 4 are:

"This challenge requires you to use some of your favourite
mathematical operators, and also get to do simple simple file IO.
Additionally you will learn bit operations.

# download this file. Process every byte, if it is greater than 30
(hex) then add 1 to it. If it is smaller than 2A (hex) then add 2 to
it.

# Reduce the filesize by half by taking every second byte out of the
file (delete the second, the fourth etc). Put the result into
part1.dat.

# Take this file AND (the & operator) each byte with part1.dat. Finish
when any end of file (whichever is first) is reached.

# Put the result into a file called part2.dat.
Take part2.dat and XOR(the ^ operator) each byte of it with its own
value and put the result into a new file called part3.dat.

# Finally take part3.dat and add (+) every byte together, append
_solution to the resulting number, and enter it in the box below."

Is it possible to do this sort of thing with python? If so, could
someone recommend a website with instructions on bit operations?
Thanks!
Jul 18 '05 #1
4 3098
Jason wrote:
Is it possible to do this sort of thing with python? If so, could
someone recommend a website with instructions on bit operations?
Thanks!

Yes. The operators in Python are the same as described by the
challenge. Bitwise and is &, bitwise or is |, and bitwise xor is ^.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ All of your moonlight whispers / Counterfeit, counterfeit love
-- Lamya
Jul 18 '05 #2

"Erik Max Francis" <ma*@alcyone.co m> wrote in message
news:41******** *******@alcyone .com...
Jason wrote:
Is it possible to do this sort of thing with python? If so, could
someone recommend a website with instructions on bit operations?
Thanks!

Yes. The operators in Python are the same as described by the
challenge. Bitwise and is &, bitwise or is |, and bitwise xor is ^.

In C, chars *are* ints (of size 1). For Python, you need to convert the
bytes to ints with ord(), do the bit manipulations, and convert back to
bytes with chr(). Make a list of bytes and ''.join(them) at the end.

And/or, you might want to use the array module.

Terry J. Reedy

Jul 18 '05 #3
Hi,

J> Take part2.dat and XOR(the ^ operator) each byte of it with its own
J> value and put the result into a new file called part3.dat.
J> # Finally take part3.dat and add (+) every byte together, append
J> _solution to the resulting number, and enter it in the box below."

I think, it's a joke. Result will be 0, python not needed.

Alexander Semenov.
Jul 18 '05 #4

"Alexander Semenov" <sa*@ulj.menate pspb.com> wrote:

Hi,

J> Take part2.dat and XOR(the ^ operator) each byte of it with its own
J> value and put the result into a new file called part3.dat.
J> # Finally take part3.dat and add (+) every byte together, append
J> _solution to the resulting number, and enter it in the box below."

I think, it's a joke. Result will be 0, python not needed.

If you go to the site, you discover the point is to work out the
implications of binary operations on values, and the value of reading
directions.
- Josiah

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.