# Check if two hexadecimal numbers are complementary in a location of a given number of bytes.

Check, using complementary code rules, if:

• if $$(9A7D)_{16}$$ and $$(7583)_{16}$$ are complementary in a location of $$2$$ bytes
• if $$(000F095D)_{16}$$ and $$(FFF0F6A3)_{16}$$ are complementary in a location of 4 bytes

I am not sure what the problem statement means exactly. What am I supposed to check? In the case of the first subpoint, I converted the two numbers into binary to see if they are complementary over a location of $$16$$ bits ($$2$$ bytes). Again, I have no idea if I am supposed to do this.

$$(9A7D)_{16} = 1001 \hspace{0.1cm} 1010 \hspace{0.1cm} 0111 \hspace{0.1cm} 1101 _ {2}$$

$$(7583)_{16} = 0111 \hspace{0.1cm} 0101 \hspace{0.1cm} 1000 \hspace{0.1cm} 0011 _ {2}$$

But in order for the two to be complementary in a location of $$2$$ bytes we would need the two numbers to be complementary as a whole, since the whole number representations have $$2$$ bytes. This looks to be false. The second subpoint of the problem reaches the same conclusion. This is what I don't think that it's right what I'm doing. It doesn't feel like I did much. So what exactly is the problem statement asking for?

• The first two bytes of the second problem are complementary: $000F_{16}+FFF0_{16}=FFFF_{16}$. Sep 30 '20 at 16:37

## 1 Answer

The problem is asking to check the two values using 1's complement and 2's complement rules.

To calculate 1's complement, subtract each digit from $$15\space(=F_{16})$$.

To calculate 2's complement, add 1 to one's complement.

$$1$$'s complement of $$9A7D_{16} = 6582_{16}$$ and not $$7583_{16}$$

$$2$$'s complement of $$9A7D_{16} = 6582_{16} + 1 = 6583_{16}$$ which is also not $$7583_{16}$$.

So, the values are neither one's complements not two's complements of each other.

Repeat the same for the 4 byte value. A cursory check shows they are 2's complements of each other.

• Can you please clarify something for me? So, in order to find $1$'s complement we take the largest value in the given base and subtract each digit from that largest value (e.g., $15$ in base $16$, $1$ in base $2$, $9$ in base $10$, and so on)? Also isn't the first digit of $1$'s complement in the exercise you solved actually $6$ and not $7$? I might be wrong, but you said to subtract each digit from 15, so we would have $15 - 9 = 6$. Am I wrong?
– user592938
Oct 1 '20 at 10:41
• Yes. That is correct. I made a math error in the first digit. My bad. So, they are not 1's or 2's complements for the first answer. But the rules are correct.
– vvg
Oct 1 '20 at 10:52