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? 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
>>>
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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))
//...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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.
|
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>...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
| |