473,804 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bit Operations

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
same as for chars.

I'm really confused on how t do it, maybe cause python is type-less (dynamic
typed)
Any Help?

Cheers,
Gianmaria
ITALY
Nov 28 '07 #1
16 6359
I'm really confused on how t do it, maybe cause python is
type-less (dynamic typed)
Being duck-typed doesn't really have anything to do with it.
Python supports logical shifting and combining
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 same as for chars.
>>a = int('1001', 2)
b = int('0110', 2)
a
9
>>b
6
>>0xff & (((0xff & a) << 4) | (0xff & b))
150

or, if you're sloppy,
>>(a << 4) | b
150

And for verification:
>>int('10010110 ', 2)
150

Thus, that can be wrapped up as a function
>>nibbles2byt e = lambda a,b: \
0xff & (((0xff & a) << 4) | (0xff & b))
>>nibbles2byte( a,b)
150
-tkc


Nov 28 '07 #2
On Nov 28, 2007 2:07 PM, Gianmaria Iaculo - NVENTA
<gi*******@hotm ail.comwrote:
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.
A string in python is a sequence of bytes, so what you're describing
here is the string "AB".
Or an other example can be i've 2 numbers.. like 7 and 8 and whant to do the
same as for chars.
"\x07\x08"
I'm really confused on how t do it, maybe cause python is type-less (dynamic
typed)
You can use the struct module to convert back and forth between byte
sequences and numerical values. For example, to get an integer with
the value of the nibble you mentioned before:

struct.unpack(" h", "AB") -(16961,)

Exactly what you'll want to use and what format you want will depend
on why you're doing this.
Nov 28 '07 #3
On Nov 28, 2007 2:27 PM, Chris Mellon <ar*****@gmail. comwrote:
On Nov 28, 2007 2:07 PM, Gianmaria Iaculo - NVENTA
<gi*******@hotm ail.comwrote:
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.

A string in python is a sequence of bytes, so what you're describing
here is the string "AB".
Ah, I didn't realize until after I'd sent this that you were trying to
merge them into the same byte. This doesn't make a whole lot of sense
- ord("A") is outside the range you can represent in half a byte - but
Python does support the full range of bitwise operations, so you can
do whatever kind of shifting and setting that you'd have done in .NET.
Nov 28 '07 #4
On Nov 29, 7:07 am, "Gianmaria Iaculo - NVENTA"
<gianma...@hotm ail.comwrote:
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.
But a nibble is 4 bits and a char in general is 8 bits. Please explain
"write A to the high nibble". Let's assume that you mean that 0 <= A
<= 15 ....

The building blocks that you need are the ord() and chr() builtin
functions, and the << (shift-left) operator. The hex() function is
useful for seeing what is happening.
>>a = '\x07'
b = '\x08'
c = 7
d = 8
ord(a)
7
>>chr(c)
'\x07'
>>hex(c)
'0x7'
>>hex(c << 4)
'0x70'
>>hex((ord(a) << 4) + ord(b))
'0x78'
>>hex((c << 4) + d)
'0x78'
>>>
Cheers,
John
Nov 28 '07 #5
On 2007-11-28, Chris Mellon <ar*****@gmail. comwrote:
On Nov 28, 2007 2:07 PM, Gianmaria Iaculo - NVENTA
<gi*******@hot mail.comwrote:
>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.

A string in python is a sequence of bytes, so what you're describing
here is the string "AB".
No, he's describing something that consists of 2 nibbles (1
byte). The string "AB" is two bytes.
>Or an other example can be i've 2 numbers.. like 7 and 8 and whant to do the
same as for chars.

"\x07\x08"
Again, he wants a single byte and that's two bytes.
>I'm really confused on how t do it, maybe cause python is
type-less (dynamic typed)

You can use the struct module to convert back and forth between byte
sequences and numerical values. For example, to get an integer with
the value of the nibble you mentioned before:

struct.unpack(" h", "AB") -(16961,)
No, he wants to do this:

(0x0A<<4) | 0x0B

(7<<4) | 8
Exactly what you'll want to use and what format you want will depend
on why you're doing this.

--
Grant Edwards grante Yow! My vaseline is
at RUNNING...
visi.com
Nov 28 '07 #6
On Wed, Nov 28, 2007 at 09:07:56PM +0100, Gianmaria Iaculo - NVENTA wrote regarding Bit Operations:
>
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
same as for chars.

I'm really confused on how t do it, maybe cause python is type-less (dynamic
typed)
Do you possibly mean that your letters are hexadecimal digits? If so, you can follow the advice given to you by others for numbers, treating your letters as numbers:

A=10
B=11
....
F=15

py>>hex(15)
'0xf'
>>int('f', 16)
15
>>int('0xf', 16)
15

Cheers,
Cliff

Nov 28 '07 #7
>>0xff & (((0xff & a) << 4) | (0xff & b))
150

or, if you're sloppy,
>>(a << 4) | b
150
Slightly OT, maybe - why exactly is the second alternative 'sloppy?'
I believe you, because I had a problem once (in Java) with bytes not
having the value I expected unless I did the and-magic, but I wasn't
clear on why. Is it an issue with the word otherwise possibly not
being zeroed out?

-dan
Nov 28 '07 #8
Txs all,
i wont to respond to who asked why i needed it:

I'm using python on GSM modules and the informations i have to move goes
along GPRS/UMTS connections so it's beatiful for me to transfer more
informations with less space...
imagine i have to send this simple data....

41.232323,12.34 5678

i can send it as it's or use the nibble trick and on the receiving station
'unlift" the data and rebuild the original information...

isn'it???

cheers + TXS,
Gianmaria

ps: now i'm gonna read all your answers in details... txs again
Firma Gianmaria Iaculo
"Gianmaria Iaculo - NVENTA" <gi*******@hotm ail.comha scritto nel messaggio
news:fi******** **@aioe.org...
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 same as for chars.

I'm really confused on how t do it, maybe cause python is type-less
(dynamic typed)
Any Help?

Cheers,
Gianmaria
ITALY

Nov 28 '07 #9
On Wed, Nov 28, 2007 at 10:05:40PM +0100, Gianmaria Iaculo - NVENTA wrote regarding Re: Bit Operations:
>
Txs all,
i wont to respond to who asked why i needed it:

I'm using python on GSM modules and the informations i have to move goes
along GPRS/UMTS connections so it's beatiful for me to transfer more
informations with less space...
imagine i have to send this simple data....

41.232323,12.34 5678

i can send it as it's or use the nibble trick and on the receiving station
'unlift" the data and rebuild the original information...

isn'it???
Um, no. It isn't. How exactly are you going to pack floating point numbers into a half a byte?

Or are you sending it as strings? Also a waste of space, and unnecessarily complex.
Nov 28 '07 #10

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

Similar topics

4
2408
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a hierarchy of algebraic matrices with the addition operation. Thus, I want to have a virtual base class class Matr;
3
2138
by: Scott Brady Drummonds | last post by:
Hello, all, My most recent assignment has me working on a medium- to large-sized Windows-based C++ software project. My background is entirely on UNIX systems, where it appears that most of my peers were writing "better" C++ code. By "better" I mean that it more regularly looked like C++ (use of objects, streams, exceptions, and ANSI-compliance) whereas some of my recent Windows-based C++ peers rely on un-portable, operating-system...
3
2250
by: ThunderMusic | last post by:
Hi, I have 2 UInt64 to add and then divide the result by another value. How can I do this? because the math operators have not been defined for UInt64. Can somebody help please? Thanks ThunderMusic
17
4676
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some analysis and I'm led to believe (but can't yet quantitatively establish as fact) that the two basic culprits are a lot of calls to: 1.) if( someString.ToLower() == "somestring" ) and
13
1921
by: Immanuel Goldstein | last post by:
Obtained under the Freedom of Information Act by the National Security Archive at George Washington University and posted on the Web today, the 74-page "Information Operations Roadmap" admits that "information intended for foreign audiences, including public diplomacy and PSYOP, increasingly is consumed by our domestic audience and vice-versa," but argues that "the distinction between foreign and domestic audiences becomes more a question...
36
2506
by: mrby | last post by:
Hi, Does anyone know of any link which describes the (relative) performance of all kinds of C operations? e.g: how fast is "add" comparing with "multiplication" on a typical machine. Thanks! -- B. Y.
3
1798
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...
6
3664
by: carsonbj | last post by:
I have an issue where the below operation works on a little-endian architecture but not on a big-endian architecture. I was under the impression that pointer arithmetic is architecture independant and bitwise operations are architecture dependant. The intention is to store two bytes, as chars, extracted from a short input parameter as: <code> void foo(short id_pair) { char *ptr = &id_pair;
4
2136
by: alex | last post by:
hi friends ... i am facing a problem while detecting floating point operations in my project, please help me. i want to find out the places in my C/C++ project where i am doing floating point operations. As it is a big project it is not possible to check every line manually, so is there any other method to detect floating point operations in my project?
0
2206
by: tabassumpatil | last post by:
Please send the c code for: 1.STACK OPERATIONS : Transfer the names stored in stack s1 to stack s2 and print the contents of stack s2. 2.QUEUE OPERATIONS : Write a program to implement DOUBLE ENDED QUEUE operations a) Use names as data to perform all operations like enqueue,dequeue,print,etc. b)Use students records as data to perform the same operations. 3.FILE OPERATIONS: write c program to implement following FILE operations:...
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
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...
1
10351
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9174
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
7638
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
5534
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4311
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
2
3834
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.