473,594 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.

Similar topics

8
3569
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, initially: dictionary = {} dictionary = old_value Then thread 1 does: v = dictionary And thread 2 does:
28
7365
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 "good" in such terms, that this core data structure is changed only by atomic operations, so that the data structure is always consistent regarding the application. Only the change-operations on the dicts and lists itself seem to cause problems...
3
1788
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 the outgoing responses (which are generated by the server). Operations have some common elements, and some which are distinct to the operation type (search, modify, compare, etc). So do responses. There are also some "operations" used...
2
1632
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
2230
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 group was into it already and is so kind to share here his experience. I have put already much efforts into this subject googling around, but up to now in vain. Best option I encountered yet is usage of the Greyware 'System Change Log' service...
11
1399
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 it's text of unknown or mixed encoding. Those include of course slicing and other operators that exist in lists, but also other operations that aren't currently defined in PEP 358, like: - str methods endswith, find, partition, replace,...
6
1547
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 when I combine couple of such operations into one line. Is it possible to overload operators, so that, I can use .+ for element wise addition, as,
7
1737
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 and unions are supposed to be done like this: In :set('casa') & set('porca') Out:set() In :set('casa') | set('porca')
16
6332
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 composed from 2 nibbles hi&low, and two chars.. like A nd B. What i wonna do is to write A to the High nibble and B to the the lower nibble. Or an other example can be i've 2 numbers.. like 7 and 8 and whant to do the
0
7941
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8246
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8368
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5738
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5404
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3895
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1476
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.