简体   繁体   中英

2's complement of a Decimal number, without converting it to binary form?

How can I convert eg n=52 (110100) into a decimal number which represents its 2's complement (ie, 12 (001100))? Is there a formula/trick using bit manipulation (using C++)?

If x-bit number then, 2^x - n will give you the 2's complement of n in decimal.

In your case, you considered 6-bit numbers, so 64 - 52 = 12 is the 2's complement of 52 .

Also note that 52 should be interpreted as -12 in signed 6-bit notation represented in 2's complement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM