Some ground rules are required.
It gets very confusing if you do not specify some basic rules about variable size and what the notation is. It is also easier to read binary if you use spaces to create groups of 4 bits.
8-bit bytes are usually defined as unsigned integers, in which case the following is valid.- 0000 0000 is zero, the smallest possible value.
- 1111 1111 is 255, the largest possible value.
- There are no negative values possible.
If you define 8-bit bytes as signed integers using complement binary, the following are valid.- 0000 0000 is zero, the smallest non-negative value.
- 0111 1111 is 127, the largest possible positive value.
- 1111 1111 is minus one.
- 1000 0000 is minus 128, the algebraically smallest value.
For both of the above, the high order bit has a weight of 128. For unsigned binary it is a positive weight. For signed complement binary it is a negative weight.
I did not analyze your algorithm carefully, but it seems to be correct.
However, when doing binary arithmetic, you should stick with binary operations. You are describing an algorithm which requires converting binary to decimal and vice versa. The conversion is tuffer than the binary operations.
Read my previous post to this thread. It is valid. Converting positive to negative uses the same algorithm as converting negative to positive.
Quote:
Change one bits to zero bits & vice versa. Then do an unsigned addition of 1 to the bit reversed value.