How to do twos complement on a 16-bit signal using Python?


If you want to get an inversion of only first 16 bits of a number, you can take a xor of that number with 65535(16 1s in binary). Forgetting a 2s complement, just add one to the result. For example,

Example

a = 3 # 11 in binary
b = (a ^ 65535) + 1
print(bin(b))

Output

This will give the output:

0b1111111111111101

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 17-Jun-2020

710 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements