This program outputs a table.
- Column 1 – number (1 to 100)
- Column 2 – Square root of the number
- For this we use built in math function – Math.Sqrt(i)
- Column 3 – Square
- Column 4 – Cube of the number
- Column 5 – Binary of the number
- Column 6 – Complement of the number
How to find the complement of a number.
First of all, you need to realize that ~ is the bitwise flip operator, which is not the same as the negate operator -. ~ does only do the bitwise flipping, but the negate operator – does bitwise flipping and add one (for integers).
As you’ve explained, if yo want to go from a postive number n to -n using the two complement method you bitwise flip/not n and add 1. ~n is just the bit-wise not meaning that ~n=-n-1.
5 = 0000 0101
Flipped (~5) = 1111 1010
So, which number does 1111 1010 represent? Since the first digit is a 1 we know it’s a negative value. To find which value, do
-(flip(1111 1010) + 1) =
-(0000 0101 + 1)
-(0000 0110) =