Velocity Reviews > Bit Operations

Bit Operations

Gianmaria Iaculo - NVENTA
Guest
Posts: n/a

 11-28-2007
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

Tim Chase
Guest
Posts: n/a

 11-28-2007
> 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

Chris Mellon
Guest
Posts: n/a

 11-28-2007
On Nov 28, 2007 2:07 PM, Gianmaria Iaculo - NVENTA
<(E-Mail Removed)> wrote:
> 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.

Chris Mellon
Guest
Posts: n/a

 11-28-2007
On Nov 28, 2007 2:27 PM, Chris Mellon <(E-Mail Removed)> wrote:
> On Nov 28, 2007 2:07 PM, Gianmaria Iaculo - NVENTA
> <(E-Mail Removed)> wrote:
> > 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.

John Machin
Guest
Posts: n/a

 11-28-2007
On Nov 29, 7:07 am, "Gianmaria Iaculo - NVENTA"
<(E-Mail Removed)> wrote:
> 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

Grant Edwards
Guest
Posts: n/a

 11-28-2007
On 2007-11-28, Chris Mellon <(E-Mail Removed)> wrote:
> On Nov 28, 2007 2:07 PM, Gianmaria Iaculo - NVENTA
><(E-Mail Removed)> wrote:
>> 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

J. Clifford Dyer
Guest
Posts: n/a

 11-28-2007
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

Dan Upton
Guest
Posts: n/a

 11-28-2007
> >>> 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

Gianmaria Iaculo - NVENTA
Guest
Posts: n/a

 11-28-2007
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" <(E-Mail Removed)> ha scritto nel messaggio
news:fikhra\$dmc\$(E-Mail Removed)...
> 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
>
>

J. Clifford Dyer
Guest
Posts: n/a

 11-28-2007
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.