473,395 Members | 1,968 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,395 software developers and data experts.

two's complement bytes

I'm dabbling with AVR's for a project I have and that means I have to
use C (ageist my will). Because my AVR will be tethered to my laptop,
I am writing most of my logic in python, in the hopes of using at
little C as possible.

In my quest I came across a need to pass a pair of sign extended two's
complement bytes. After painfully reading the wikipedia article on
what two's complement was, I then thought of how I would handle this
in python. I don't really recall ever having to work in binary with
python, so I really am clueless on what to do.

I can feed python either two hex bytes or binary, but how do I convert
it into an int, and more importantly how do I make sure it handles the
sign properly?
Aug 24 '08 #1
4 4648
On Aug 23, 10:51*pm, "Adam W." <AWasile...@gmail.comwrote:
I'm dabbling with AVR's for a project I have and that means I have to
use C (ageist my will). *Because my AVR will be tethered to my laptop,
I am writing most of my logic in python, in the hopes of using at
little C as possible.

In my quest I came across a need to pass a pair of sign extended two's
complement bytes. *After painfully reading the wikipedia article on
what two's complement was, I then thought of how I would handle this
in python. *I don't really recall ever having to work in binary with
python, so I really am clueless on what to do.

I can feed python either two hex bytes or binary, but how do I convert
it into an int, and more importantly how do I make sure it handles the
sign properly?
Try this out. Does it come close to what you want?

import struct
struct.pack( 'i', ~10 )
~struct.unpack( 'i', _ )[ 0 ]

>>import struct
struct.pack( 'i', ~10 )
'\xf5\xff\xff\xff'
>>~struct.unpack( 'i', _ )[ 0 ]
10
>>>
Aug 24 '08 #2
On Aug 24, 12:23*am, castironpi <castiro...@gmail.comwrote:
Try this out. *Does it come close to what you want?

import struct
struct.pack( 'i', ~10 )
~struct.unpack( 'i', _ )[ 0 ]


>import struct
struct.pack( 'i', ~10 )
'\xf5\xff\xff\xff'
>~struct.unpack( 'i', _ )[ 0 ]
10- Hide quoted text -

- Show quoted text -
Humm, so how do you use it :P Let me give you some examples and then
you can run it through:

0b1111110010010000 or 0xFC90 Should equal -880
0b0000011111010000 or 0x07D0 Should equal +2000
Aug 24 '08 #3
On Aug 23, 11:52*pm, "Adam W." <AWasile...@gmail.comwrote:
On Aug 24, 12:23*am, castironpi <castiro...@gmail.comwrote:
Try this out. *Does it come close to what you want?
import struct
struct.pack( 'i', ~10 )
~struct.unpack( 'i', _ )[ 0 ]
>>import struct
>>struct.pack( 'i', ~10 )
'\xf5\xff\xff\xff'
>>~struct.unpack( 'i', _ )[ 0 ]
10- Hide quoted text -
- Show quoted text -

Humm, so how do you use it :P *Let me give you some examples and then
you can run it through:

0b1111110010010000 or 0xFC90 *Should equal -880
0b0000011111010000 or 0x07D0 *Should equal +2000
In this case I look at:
>>struct.unpack( '>h', '\xfc\x90' )[0]
-880
>>struct.unpack( '>h', '\x07\xd0' )[0]
2000
Aug 24 '08 #4
On Aug 24, 1:11*am, castironpi <castiro...@gmail.comwrote:
On Aug 23, 11:52*pm, "Adam W." <AWasile...@gmail.comwrote:


On Aug 24, 12:23*am, castironpi <castiro...@gmail.comwrote:
Try this out. *Does it come close to what you want?
import struct
struct.pack( 'i', ~10 )
~struct.unpack( 'i', _ )[ 0 ]
>import struct
>struct.pack( 'i', ~10 )
'\xf5\xff\xff\xff'
>~struct.unpack( 'i', _ )[ 0 ]
10- Hide quoted text -
- Show quoted text -
Humm, so how do you use it :P *Let me give you some examples and then
you can run it through:
0b1111110010010000 or 0xFC90 *Should equal -880
0b0000011111010000 or 0x07D0 *Should equal +2000

In this case I look at:
>struct.unpack( '>h', '\xfc\x90' )[0]
-880
>struct.unpack( '>h', '\x07\xd0' )[0]

2000- Hide quoted text -

- Show quoted text -
Perfect, thank you! I will have to read up on struct to see how you
did that.
Aug 24 '08 #5

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

Similar topics

4
by: aling | last post by:
Given: signed a, b; How to judge overflow of the sum of these two operands? Just use one *if* statement. Is this statement right? if ((a>0 && b>0 && sum<=0) || (a<0 && b<0 && sum>=0)) //...
8
by: Mantorok Redgormor | last post by:
From least to greatest is it sign magnitude ones complement two's complement Where sign magnitude is the least way to represent integers and two's complement is the best way to represent...
7
by: Greenhorn | last post by:
Hi, Is two's complement always used as a storage method or is it computed while computing the expression involved. e.g., int a = -2, b = 3, c = 4, d; d = b - c; Here, is 'a' stored as two's...
88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
3
by: Marc | last post by:
I'm trying to set the first three bits to zero on a byte. For some reason, the compiler is casting the number peculiarly when I use the bitwise complement operator. Here's what I think should...
22
by: sarathy | last post by:
Hi all, I have a few doubts in the 1's and 2's complement representation. Generally negative numbers can be represented using either 1's complement or 2's complement representation. 1's...
14
by: darthghandi | last post by:
What would be the most efficient way to calculate the two's complement of a variable length byte array? Thanks for your time.
29
by: lovecreatesbea... | last post by:
The following function determines the maximum of two integers. It works on my machine. If (a - a) is negative, what's the first bit of: (unsigned)(a - a)? Is it 0 or 1? #include <limits.h>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.