2

I'm supposed to write a program in C that switches hex number to its 2's complement. e.g) 0000008f to ffffff71

so far I have figured out how to print out 2'c complement of binary, but hex seems like a different story to me. Any suggestions about which direction I'm suppose to looking at?

MerZer0
  • 25
  • 1
  • 1
  • 5

1 Answers1

2

To compute the two’s complement of a n-digit hexadecimal numeral, either:

  • complement each digit (exchange 0 for F, 1 for E, and so on) and then add one to the whole numeral, or
  • subtract the numeral from (In hexadecimal) one followed by n zeroes.

If the number is not a whole number of hexadecimal digits, some adjustments to the above must be made for the first digit:

  • For the first method, complement the first digit using the appropriate number of bits. For example, with 22 bits, the first hexadecimal digit uses only two bits, so exchange 0 for 3, 1 for 2, 2 for 1, and 3 for 0.
  • For the second method, subtract from the appropriate power of two. For example, for 22 bits, subtract from (hexadecimal) 400000.
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312