Mastering Reverse Engineering
上QQ阅读APP看书,第一时间看更新

Signed numbers

Binary numbers can be structured as signed or unsigned. For signed numbers or integers, the most significant bit dictates what sign the number is in. This requires a defined size of the binary such as BYTE, WORD, DWORD, and QWORD. A BYTE has a size of 8 bits. A WORD has 16 bits while a DWORD (double WORD) has 32 bits. A QWORD (quad WORD) has 64 bits. Basically, the size doubles as it progresses.

In our example, let's use a BYTE. Identifying a positive binary number is easy. In positive numbers, the most significant bit, or 8th bit in a byte, is 0. The rest of the bits from 0 to the 7th bit is the actual value. For a negative binary number, the most significant bit is set to 1. However, the value set from 0 to the 7th bit is then calculated for a two's complement value:

01011011b = +91
11011011b = -37
10100101b = -91
00100101b = +37

The "2's complement" of a value is calculated in two steps:

  1. Reverse 1s and 0s, so that 1 becomes 0 and 0 becomes 1, for example, 1010b becomes 0101b. This step is called the one's complement.
  2. Add 1 to the result of the previous step, for example, 0101b + 1b = 0110b.

To write down the binary equivalent of -63, assuming it is a BYTE, we only take bits 0 to 7:

  1. Convert into binary using the previous procedure:
63 = 0111111b
  1. Do "1's complement" as follows:
0111111b -> 1000000b
  1. Add 1 to the preceding outcome to get the "2's complement" result:
1000000b + 1 = 1000001b
  1. Since this is a negative number, set the most significant bit to 1:
11000001b = -63

Here's how to write the decimal of a negative binary number:

  1. Take note that the significant bit is 1, and so a negative sign:
10111011b
  1. Take the "1's complement," then add 1:
  01000100b
+ 1b
01000101b
  1. Convert the result to decimal, and place the – sign at the beginning, since this is a negative number:
- 01000101b = -69