bit operations with python?

J

Jason

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!
 
E

Erik Max Francis

Jason said:
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 ^.
 
T

Terry Reedy

Erik Max Francis said:
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
 
A

Alexander Semenov

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.
 
J

Josiah Carlson

Alexander Semenov said:
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
 

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

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top