473,406 Members | 2,956 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,406 software developers and data experts.

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 6316
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
>>nibbles2byte = 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*******@hotmail.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*******@hotmail.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...@hotmail.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*******@hotmail.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.345678

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*******@hotmail.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.345678

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
On Nov 28, 2007 3:18 PM, J. Clifford Dyer <jc*@sdf.lonestar.orgwrote:
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.345678

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.
Assuming these are coordinates, not floats, using strings makes sense
but the zlib module is probably a much better choice than a
hand-written compression scheme.
Nov 28 '07 #11
U are really nice guys... i'm really apreciating (sorry 4 my bad english)

Chriss is right this are coordinates.... and i'm treating as strings
naturally
I dont really have floating points on my module.. it run a 1.5 python
version from Telit.
So i dont have zLib too... just have 1.5 Mb of Ram and 3Mb of Rom... not
realy confortable..isn't it?

I'm tring some experiments on the command line... i've tried this:

My longitude is 42.237897

so as a first step... i created a X and done this job as your examples:

a = 4
b = 2

x = (a<<4)|b
x is 66

so i can do:

aDecoded = x >4

and i have the 4 again...( a value) but i've some problems while i decode
the b....

Where i go wrong?
Gianmaria


Firma Gianmaria Iaculo
"Chris Mellon" <ar*****@gmail.comha scritto nel messaggio
news:ma***************************************@pyt hon.org...
On Nov 28, 2007 3:18 PM, J. Clifford Dyer <jc*@sdf.lonestar.orgwrote:
>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.345678

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.

Assuming these are coordinates, not floats, using strings makes sense
but the zlib module is probably a much better choice than a
hand-written compression scheme.
Nov 28 '07 #12
> >>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?
Whoops...extra "f"s slipped into my nibble-mask

"Sloppy" lets through things like
>>a = int('11111', 2) # overflows a nibble
b = int('11111', 2)
(a<<4) | b
511
>>0xff & (((0xf & a) << 4) | (0xf & b))
255

It clamps each nibble to a true nibble, and the output to a true
byte. If you validate your nibbles, you could be lazy yet
accurate with
>>result = ((0xf & a) << 4) | (0xf & b)
result
255

To get the nibbles back out of the resulting byte, one can simply
>>a = 0xf & (result >4)
b = result & 0xf
-tkc


Nov 28 '07 #13
On Nov 29, 8:05 am, "Gianmaria Iaculo - NVENTA"
<gianma...@hotmail.comwrote:
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.345678

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???
Sorry, but it's not apparent what you propose to do. If each number
has 8 decimal digits of precision (as in your example), you could
possibly get by with a 32-bit floating point number. If it's always 6
decimal places and 0 <= number < 1000, you could pack (number *
1000000) into a 32-bit integer. For the above two options, check out
the struct module. OTOH, maybe it's "packed decimal" that you mean --
try Googling that phrase and see if it matches your intentions. If it
does, and you are concerned with speed, a 100-element dictionary
mapping each byte-pair to a packed byte might be a good idea instead
of the bit bashing:
convert = {
'78': '\x78',
...
}
See http://mail.python.org/pipermail/pyt...er/056329.html

HTH,
John

Nov 28 '07 #14
On Nov 29, 8:35 am, "Gianmaria Iaculo - NVENTA"
<gianma...@hotmail.comwrote:
U are really nice guys... i'm really apreciating (sorry 4 my bad english)

Chriss is right this are coordinates.... and i'm treating as strings
naturally
I dont really have floating points on my module.. it run a 1.5 python
version from Telit.
So i dont have zLib too... just have 1.5 Mb of Ram and 3Mb of Rom... not
realy confortable..isn't it?

I'm tring some experiments on the command line... i've tried this:

My longitude is 42.237897

so as a first step... i created a X and done this job as your examples:

a = 4
b = 2

x = (a<<4)|b
x is 66

so i can do:

aDecoded = x >4

and i have the 4 again...( a value) but i've some problems while i decode
the b....

Where i go wrong?
>>66 & 0xf
2
>>>
Nov 28 '07 #15
John can you make an example of this solution? You maen that a more compact
way is possible???
Firma Gianmaria Iaculo
"John Machin" <sj******@lexicon.netha scritto nel messaggio
news:e6**********************************@e10g2000 prf.googlegroups.com...
On Nov 29, 8:05 am, "Gianmaria Iaculo - NVENTA"
<gianma...@hotmail.comwrote:
>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.345678

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???

Sorry, but it's not apparent what you propose to do. If each number
has 8 decimal digits of precision (as in your example), you could
possibly get by with a 32-bit floating point number. If it's always 6
decimal places and 0 <= number < 1000, you could pack (number *
1000000) into a 32-bit integer. For the above two options, check out
the struct module. OTOH, maybe it's "packed decimal" that you mean --
try Googling that phrase and see if it matches your intentions. If it
does, and you are concerned with speed, a 100-element dictionary
mapping each byte-pair to a packed byte might be a good idea instead
of the bit bashing:
convert = {
'78': '\x78',
...
}
See http://mail.python.org/pipermail/pyt...er/056329.html

HTH,
John
Nov 28 '07 #16
On Nov 29, 9:20 am, "Gianmaria Iaculo - NVENTA"
<gianma...@hotmail.comwrote:
John can you make an example of this solution?
Which possible solution? (a) 32-bit floating point (b) 32-bit integer
(c) packed decimal
You maen that a more compact
way is possible???
More compact than what? If your coordinates are all 8-digit decimal
numbers, then each of the above possible solutions will take 32 bits
per number. Which you could use depends on the (unstated) range and
precision of your coordinates. Also, you'll maybe notice that my
comment about using a dictionary for possible solution (c) was a
possible SPEED enhancement, not a compression enhancement.
Nov 28 '07 #17

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

Similar topics

4
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...
3
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...
3
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 ...
17
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...
13
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...
36
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!...
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...
6
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...
4
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.