-1

Here's what I have so far:

decimalEquivalent is variable that represents an integer.

#One's complement of the binary string is shown
onesComplement = bin(~decimalEquivalent)
print(f'The negative no (-{decimalEquivalent}) using 1\'s Complement: {onesComplement}')

#Two's complement of the binary string is shown
twosComplement = onesComplement + bin(1)
print(f'The negative no (-{decimalEquivalent}) using 2\'s Complement: {twosComplement}')

Could you help me figure out what I am doing wrong?

I was trying to determine one's complement and two's complement for an integer.

1 Answer 1

1

bin returns a string. You need to do all of your arithmetic before calling bin, or you'll just be concatenating strings. Consider

#One's complement of the binary string is shown
onesComplement = ~decimalEquivalent
print(f'The negative no (-{decimalEquivalent}) using 1\'s Complement: {bin(onesComplement)}')

#Two's complement of the binary string is shown
twosComplement = onesComplement + 1
print(f'The negative no (-{decimalEquivalent}) using 2\'s Complement: {bin(twosComplement)}')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.