Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: Binary number manipulation

Reply
Thread Tools

RE: Binary number manipulation

 
 
SBrunning@trisystems.co.uk
Guest
Posts: n/a
 
      12-01-2003
> From: Matthew A. Berglund [SMTP:(E-Mail Removed)]
> I am very new to python so I am working on some ass-umptions which may be
> incorrect (please forgive me).
>
> 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.
It is intended solely for the addressee. Access to this email by anyone else
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.

 
Reply With Quote
 
 
 
 
Tim Roberts
Guest
Posts: n/a
 
      12-03-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) 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, (E-Mail Removed)
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
 
 
 
Georgy Pruss
Guest
Posts: n/a
 
      12-03-2003

"Tim Roberts" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
| (E-Mail Removed) 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


 
Reply With Quote
 
Bill Sneddon
Guest
Posts: n/a
 
      12-03-2003
Georgy Pruss wrote:
> "Tim Roberts" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> | (E-Mail Removed) 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


 
Reply With Quote
 
Matthew A. Berglund
Guest
Posts: n/a
 
      12-06-2003
Thanks everybody.

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

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

Thanks,
Matt
 
Reply With Quote
 
Tim Roberts
Guest
Posts: n/a
 
      12-07-2003
"Matthew A. Berglund" <(E-Mail Removed)> wrote:
>
>Thanks everybody.
>
>I settled on the
>((high << | 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, (E-Mail Removed)
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
conversion of real number into binary number rajeswari01 Hardware 1 07-15-2011 05:30 AM
conversion of real number into binary number rajeswari01 Hardware 0 10-15-2010 12:30 AM
How do you convert a hexadecimal number to a binary number? Bob Sanders Ruby 5 05-30-2008 11:50 AM
Convert decimal number in binary number makok VHDL 1 02-23-2004 06:04 PM
Binary number manipulation Matthew A. Berglund Python 1 11-28-2003 09:21 PM



Advertisments