473,385 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 3082
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.com> 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.menatepspb.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.

Similar topics

8
by: Glenn Kasten | last post by:
I am wondering which operations in Python are guaranteed to be atomic in the presence of multi-threading. In particular, are assignment and reading of a dictionary entry atomic? For example,...
28
by: robert | last post by:
In very rare cases a program crashes (hard to reproduce) : * several threads work on an object tree with dict's etc. in it. Items are added, deleted, iteration over .keys() ... ). The threads are...
3
by: Hallvard B Furuseth | last post by:
I'm wondering how to design this: An API to let a request/response LDAP server be configured so a user-defined Python module can handle and/or modify some or all incoming operations, and later...
2
by: A.M | last post by:
Hi, Is there any online resource that gives examples about advanced python string, list and map operations? Today I saw this and I found that I have to work more on mentioned topics:
9
by: Claudio Grondi | last post by:
I am aware, that it is maybe the wrong group to ask this question, but as I would like to know the history of past file operations from within a Python script I see a chance, that someone in this...
11
by: Gerrit Holl | last post by:
Hi, In Python 3, reading from a file gives bytes rather than characters. Some operations currently performed on strings also make sense when performed on bytes, either if it's binary data or if...
6
by: jm.suresh | last post by:
Hi, Frequently I get to do like this: a = (1, 2, 3, 4) # some dummy values b = (4, 3, 2, 1) import operator c = map(operator.add, a, b) I am finding the last line not very readable especially...
7
by: Flavio | last post by:
Hi, I have been playing with set operations lately and came across a kind of surprising result given that it is not mentioned in the standard Python tutorial: with python sets, intersections ...
16
by: Gianmaria Iaculo - NVENTA | last post by:
Hi there, I'm so new to python (coming from .net so excuse me for the stupid question) and i'm tring to do a very simple thing,with bytes. My problem is this: i've a byte that naturally is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.