# RE: Binary number manipulation

Discussion in 'Python' started by SBrunning@trisystems.co.uk, Dec 1, 2003.

1. ### Guest

> From: Matthew A. Berglund [SMTP:]
> I am very new to python so I am working on some ass-umptions which may be
>
> I am working on a problem that requires me to take 2 decimal integers,
> each of which represents half of a word. I need to slap these things
> together and get out another decimal integer.
> A couple of questions:
> 1. Is there a way to typecast a variable as a binary?

AFAIK, no.

> 2. If 1 is no, does that mean that I need to do all the manipulation in
> some icky string format and then go back?

I use:

((high << 16) | low)

> I do see the bit-wise operations available and this looks like I need to
> simply perform these on my integers? Can it really be that simple?

Yup.

Cheers,
Simon Brunning,
http://www.brunningonline.net/simon/blog/
--LongSig

-----------------------------------------------------------------------
The information in this email is confidential and may be legally privileged.
is unauthorised. If you are not the intended recipient, any disclosure,
copying, distribution, or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot
accept liability for statements made which are clearly the senders own.

, Dec 1, 2003

2. ### Tim RobertsGuest

wrote:
>
>> 2. If 1 is no, does that mean that I need to do all the manipulation in
>> some icky string format and then go back?

>
>I use:
>
>((high << 16) | low)

....which, unfortunately, gives a FutureWarning in Python 2.3 if high
happens to be larger than 32767.

I think we're all going to be using a lot more of the struct module in the
future. For example, this is the equivalent of what you posted:

struct.unpack('>L',struct.pack('>2H',high,low))[0]

I may have to start using things like:

def MAKELONG(high,low):
return struct.unpack('>L',struct.pack('>2H',high,low))[0]

Is there a smarter way to do this kind of thing? This also works:
((high << 16L) | low)
--
- Tim Roberts,
Providenza & Boekelheide, Inc.

Tim Roberts, Dec 3, 2003

3. ### Georgy PrussGuest

"Tim Roberts" <> wrote in message news:...
| wrote:
| >
| >> 2. If 1 is no, does that mean that I need to do all the manipulation in
| >> some icky string format and then go back?
| >
| >I use:
| >
| >((high << 16) | low)
|
| ...which, unfortunately, gives a FutureWarning in Python 2.3 if high
| happens to be larger than 32767.

Yes, it's really annoying. I use (high * 65536 + low) instead.

-Georgy

Georgy Pruss, Dec 3, 2003
4. ### Bill SneddonGuest

Georgy Pruss wrote:
> "Tim Roberts" <> wrote in message news:...
> | wrote:
> | >
> | >> 2. If 1 is no, does that mean that I need to do all the manipulation in
> | >> some icky string format and then go back?
> | >
> | >I use:
> | >
> | >((high << 16) | low)
> |
> | ...which, unfortunately, gives a FutureWarning in Python 2.3 if high
> | happens to be larger than 32767.
>
> Yes, it's really annoying. I use (high * 65536 + low) instead.
>
> -Georgy
>
>

Were are you getting you numbers from?
Have you looked at the struct module?

I am reading a binary file and use this command from struct module to
convert to integer not sure if this helps you or not.

mwB = struct.unpack('>H',mw)[0] # unpack big-endian unsigned short
...detuple it

Bill Sneddon, Dec 3, 2003
5. ### Matthew A. BerglundGuest

Thanks everybody.

I settled on the
((high << 8) | low)
As this fits my need for the moment.

I will look at struct when I have some more time.

Thanks,
Matt

Matthew A. Berglund, Dec 6, 2003
6. ### Tim RobertsGuest

"Matthew A. Berglund" <> wrote:
>
>Thanks everybody.
>
>I settled on the
>((high << 8) | low)
>As this fits my need for the moment.

But this solves a very different problem from the one you originally
posted, which was ((high << 16) | low). If you need to combine WORDS, then
the solution you have settled on will produce the wrong answers.
--
- Tim Roberts,
Providenza & Boekelheide, Inc.

Tim Roberts, Dec 7, 2003