Python Forum
Find the complement of a number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find the complement of a number
#1
I want to find the complement of a number
For example:

The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.

From: https://www.tutorialspoint.com/python/bi...xample.htm
It suggests it just need to use "~". It shows:

a=60

bin(a)=0011 1100 
bin(~a)= 1100 0011=-61
But on my python3.5, what I get is:
a=60
bin(a)=0011 1100 
bin(~a)= 111101=-61
The resulted number -61 is right, but its binary form is not complement of a.

What happened?

Thanks,

L
Reply
#2
Hi there,


it is explained very well here:
http://superuser.com/questions/975684/co...-to-binary

There is a small difference between the 1's and 2's complement.
The 1's complement simply switches 0->1 and 1->0, like 1010 becomes 0101.
With the 2's complement you proceed the same way, but add 1 in the end: 1010->0101 (1's complement) +0001 -> 0110 (2's complement).

Now you can represent positive numbers. What about negatives? We can assign the first bit to the sign. That means 0 for + and 1 for -:
1111 1111 to 0111 1111 (8 bit := 1 Byte); that is what you can read detailed above.

To answer your question:
60 (dec) -> 0011 1100 (bin)
1's complement: 1100 0011, that means the first 1 -> - and 100 0011 is 67. You work with one Byte (=8bit), 1 is already assigned, so 7 left. 2**7 = 128. You now substract (because this is a negative value and this is how negative values are represented in binary) this value of 67; 67-128 = -61.


With Python 3.5:
>>> bin(~60)
'-0b111101'
>>> format(~60, 'b')
'-111101'
The sign is already stored separately, so you just get the binary form of 60 and the "2's complement" by adding 1: 61.
I have no detailed knowledge how python stores bits, but from its behavior I suggest this result.

Hope my explanation was understandable.
Reply
#3
(Jan-19-2017, 01:42 PM)Aquaplant Wrote: '-0b111101'
Thanks for the information. Now I understand there are varied ways of complement.

But I still don't know how to convert '0011 1100' to '1100 0011' with the built in function have in Python 3.5

Any idea?
Reply
#4
You can implement it.

def bswitch(num):
    c = 1

    while num*2 > c:
        num = num ^ c
        c = c << 1

    return num
Output:
>>> bswitch(10) 5 >>> bin(10) '0b1010' >>> bin(5) '0b101' >>> bswitch(32) 31 >>> bin(32) '0b100000' >>> bin(31) '0b11111' >>>
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
''.join([str((int(c) ^ 1)) for c in "00111100"])

This should do it
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  find the sum of a series of values that equal a number ancorte 1 491 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,184 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Find if chain of characters or number Frankduc 4 1,791 Feb-11-2022, 01:55 PM
Last Post: Frankduc
Question Help to find the largest int number in a file directory SalzmannNicholas 1 1,625 Jan-13-2022, 05:22 PM
Last Post: ndc85430
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,407 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  Find Average of User Input Defined number of Scores DustinKlent 1 4,280 Oct-25-2019, 12:40 AM
Last Post: Larz60+
  find nearest number d3fi 7 3,937 Aug-26-2019, 09:32 PM
Last Post: ichabod801
  Find the second largest number DarkCraftPlayz 8 11,286 May-29-2019, 02:46 AM
Last Post: heiner55
  Find index of missing number parthi1705 3 3,140 May-07-2019, 10:52 AM
Last Post: avorane
  Find number in a text for if statement BitbyBit 3 3,249 Jul-13-2018, 04:38 PM
Last Post: BitbyBit

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020