This is the second day of my participation in Gwen Challenge

Source code, inverse code, complement code, shift code

For ease of expression, it is represented as a byte with 8 bits.

The original code

The source code is to convert a number to a binary representation, change the highest bit to a sign bit, and set it to 0 for a positive number and 1 for a negative number. For example: The +1 binary is 0000 0001, the highest bit is the sign bit 0, 0000 0001 -1 the +1 binary is 0000 0001, the highest bit is the sign bit 1000 0001 and now the interesting thing is, what about +0 and -0? +0 with the original code is expressed as 0000 0000 -0 with the original code is expressed as 1000 0000 is very strange ah, the same 0 is expressed in the original code is not the same, this is also a disadvantage of the original code, which also fofods the appearance of the complement.Copy the code

Before we get to complement, let’s first learn what inverse code is.

Radix-minus-one complement

The inverse of a positive number is the same as the original code. The inverse of a negative number is that the highest bit of the original code remains unchanged, that is, the sign bit, and the remaining bits are reversed. For example, the inverse code of +1 is 0000 0001. The original code of -1 is 1000 0001, and the inverse code is 1111 1110Copy the code

After learning inverse code, it is much easier to learn complement.

complement

The complement of a positive number is the same as the original; The complement of a negative number is its inverse digit plus 1. For example, the complement of +1 is 0000 0001-1 and the complement of +1 is 1111 1111. Then you find that the complement of +0 and -0 is only one: 0000 0000. The consistency of numbers brings great convenience to the operation of complement codes.Copy the code

The last one is shift code, which is used in floating-point arithmetic.

frameshift

The shift code is used to act as a step code in floating-point operations, which I'll discuss in more detail later. Shift code is the inverse of the sign bit of the complement. For example, the complement of +1 is 0000 0001, the complement of 1000 0001-1 is 1111 1111, and the shift is 0111 1111Copy the code

So, what is the range of source, inverse and complement? You can refer to the table below

When n is 8 the source range is -127 to 127 the inverse range is -127 to 127 and the complement range is -128 to 127 you can see that the complement range is slightly larger than the source and the inverse, why is that? The reason is that the original code and the inverse code +0 and -0 are different, occupying one more code bit at a time, which you only need to understand.Copy the code

Next time, I’ll talk about the representation of data — floating-point operations, which is also an important section, and I welcome you to come and correct it.

The above is the source code, inverse code, complement and shift code overview, welcome to comment. If there are any mistakes, please correct them.