two's complement of numbers in python

16,322

Solution 1

If you're doing something like

format(num, '016b')

to convert your numbers to a two's complement string representation, you'll want to actually take the two's complement of a negative number before stringifying it:

format(num if num >= 0 else (1 << 16) + num, '016b')

or take it mod 65536:

format(num % (1 << 16), '016b')

Solution 2

The two's complement of a value is the one's complement plus one.

You can write your own conversion function based on that:

def to_binary(value):
    result = ''
    if value < 0:
        result = '-'
        value = ~value + 1
    result += bin(value)
    return result

The result looks like this:

>>> to_binary(10)
'0b1010'
>>> to_binary(-10)
'-0b1010'

Edit: To display the bits without the minus in front you can use this function:

def to_twoscomplement(bits, value):
    if value < 0:
        value = ( 1<<bits ) + value
    formatstring = '{:0%ib}' % bits
    return formatstring.format(value)

>>> to_twoscomplement(16, 3)
'0000000000000011'
>>> to_twoscomplement(16, -3)
'1111111111111101'
Share:
16,322

Related videos on Youtube

user3299406
Author by

user3299406

Updated on October 17, 2022

Comments

  • user3299406
    user3299406 over 1 year

    I am writing code that will have negative and positive numbers all 16 bits long with the MSB being the sign aka two's complement. This means the smallest number I can have is -32768 which is 1000 0000 0000 0000 in two's complement form. The largest number I can have is 32767 which is 0111 1111 1111 1111.

    The issue I am having is python is representing the negative numbers with the same binary notation as positive numbers just putting a minus sign out the front i.e. -16384 is displayed as -0100 0000 0000 0000 what I want to be displayed for a number like -16384 is 1100 0000 0000 0000.

    I am not quite sure how this can be coded. This is the code i have. Essentially if the number is between 180 and 359 its going to be negative. I need to display this as a twos compliment value. I dont have any code on how to display it because i really have no idea how to do it.

    def calculatebearingActive(i):
    
        numTracks = trackQty_Active
        bearing = (((i)*360.0)/numTracks)
        if 0< bearing <=179:
            FC = (bearing/360.0)
            FC_scaled = FC/(2**(-16))
            return int(FC_scaled)
    
        elif 180<= bearing <=359:
            FC = -1*(360-bearing)/(360.0)
            FC_scaled = FC/(2**(-16))
            return int(FC_scaled)
    
        elif bearing ==360:
            FC = 0
            return FC
    
    • Ayman
      Ayman about 10 years
      Are you concerned with displaying only or do you also want to perform calculations?
    • zhangxaochen
      zhangxaochen about 10 years
      why do you insist on editing 'complement' to 'compliment'?
  • user3299406
    user3299406 about 10 years
    if an interface was going to unpack that would it see a two's compliment number? What i mean is would it know that the value packed is meant to be a negative?
  • zhangxaochen
    zhangxaochen about 10 years
    format(num if num >= 0 else (1 << 16) - num, '016b') gets '10100000000000000' for num=-16384, while the OP wants '1100 0000 0000 0000'
  • user2357112
    user2357112 about 10 years
    @zhangxaochen: Whoops. That - needs to be a +. Fixed.
  • user3299406
    user3299406 about 10 years
    this does exactly what i want. Thankyou so much
  • dcoles
    dcoles over 8 years
    You can also do format(num & 0xffff, '016b') to do so using a bitmask.