Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Dear Please help me to get the explanation output. Please provide me the code fo

ID: 3745522 • Letter: D

Question

Dear Please help me to get the explanation output. Please provide me the code for the explanation. Here are some example of the output how it should be look like.

Example 1:

Input: 5

Output: 3

Explanation: The binary representation of 5 is 101 (no leading zero bits), and its co mplement is 010. So you need to add 1 to 010 resulting to 011 i.e. 3

Example 2:

Input: 10

Output: 6

Explanation: The binary representation of 10 is 1010 (no leading zero bits), and its complement is 101. So you need to add 1 to 101 resulting to 110 i.e. 6

def find2sCompliment(num):

numWorld=bin(num) #convert decimal number to binary represenetation
numWorld=numWorld[2:] #trims initial 2 chars from the string
length=len(numWorld) #calculates the length of the string

i=0
res=''

#Following loop would iterate for every char to convert it.


while i < length:
if numWorld[i]=='1': #if number is 1
res += '0' #suffix 0 to res string
else: # if number is 0
res+='1' #suffix 1 to string
i+=1   
return int(res,2) #convert the res string to binary

num = int(input("Enter a number: "))   
res = find2sCompliment(num)

#display the 2's compliment of the number by adding 1
print("2's compliment of ", num, " is = ", res+1)

Explanation / Answer

The complement of a binary number is computed by converting all 1's to 0 and all 0's to 1. Meaning just have to flip the binary digits of that number. Explanation from your example:

Example1: Binary of representation of 5 is 101. So to find its complement(also known as 1's complement) we just have to change all 1's to 0 and all 0's to 1. After doing that it will look like 010, which is binary representation of integer 2. So now we have found that 1complement of 5 is 2. To find 2's complement we just have add 1 to the complement of the number. Here complement of 5 is 2, so adding 1 to it gives 3 which is the output.

Examle2: Similarly as Example 1, binary of representation of 10 is 1010. So to find its complement we just have to change all 1's to 0 and all 0's to 1. After doing that it will look like 0101, which is binary representation of integer 5. So now we have found that complement of 10 is 5. To find 2's complement we just have add 1 to the complement of the number. Here complement of 10 is 5, so adding 1 to it gives 6 which is the output.

The code you have pasted is correct. I am just copying the code below and provide just a bit of additional explanation, some of which are already given as comments.

Please note your function find2sCompliment(num) is actually computing 1's complement and returning it, then at very last of your code you are adding 1 to get 2's complement.

def find2sCompliment(num):

    # The below predefined/inbuilt bin() function takes a number and returns it's binary representation as a string with '0b' prefixed to it. The prefixed '0b' means that the result is binary. So we need to trim this '0b' from the result to get our actual binary, which we are doing in next line. For example, bin(10) will return '0b1010'. So we will trim the first 0b, i.e first 2 characters to get actual binary of 10 which is 1010.
    numWorld=bin(num) # convert decimal number to binary represenetation
    numWorld=numWorld[2:] # trims initial 2 chars from the string which is '0b'
    length=len(numWorld) # calculates the length of the string

    i=0
    res=''

    # Following loop would iterate for every character in the string "numWorld" (as it is string which contains binary representation of the number) and if the character is 1 change it to 0, else if the character is 0 change it to 1(Remember for complement we just have to change all 1's to 0 and all 0's to 1.

    while i < length:
        if numWorld[i]=='1': # if number is 1 below line will change it to 0
            res += '0' #suffix 0 to res string
        else: # if number is 0 below line will change it to 0
            res+='1' #suffix 1 to string
        i+=1

# Below line is taking the complement which is in binary, of the number which is now stored in variable string 'res' and passing it to int() function to get the decimal number of that binary. The second argument 2 means we are coverting the 'res' to base 2, which will give us decimal
    return int(res,2) # convert the res string to binary

# Prompt the user to input the number for which 2's complement is required, convert it to a integer using int() as the number inputed will be as a string

num = int(input("Enter a number: "))  
res = find2sCompliment(num) # Pass the number to the function find2sCompliment(num)

# display the 2's compliment of number by adding 1 to the returned 1's complement number as 'res'
print("2's compliment of ", num, " is = ", res+1)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote