I am working on a program in Cryptography. It is better to say that I have designed a new algorithm. I want to implement it in C. Can anyone give ideas how to create the 'keys'?
@rijinpk1 - ~ (not) operation is not working. Please suggest something. If I do ~1 the output obtained is -2.
int main()
{
int a=~1;
printf("%d",a);
return 0;
}
This is the program. The o/p is -2 and when ~0 is performed the o/p is -1.
-2 is infact the right answer. This is the answer which you should actually expect as you are using the BITWISE NOT operator.
What you are using is the
bitwise NOT operator. This is not the logical NOT operator
! Emphasis on the exclamation mark. Had it been ! then you would have got zero.
a was earlier 1.
or I should say, it was 00000000000000000000000000000001. (Note: These are 32 bits in total.)
Then you did a NOT gate to every digit, so it became 11111111111111111111111111111110. This is actually Two's compliment of the number 2. This is the representation of -2.)
Have you heard about
Two's compliment? This is used to represent negative numbers in computers.
Two's compliment is basically flipping all digits reverse (0 to 1 and 1 to 0) and then adding 1.
So, how would we get the original positive represenation from Two's Compliment? We'll get it by subtracting 1 and then flipping all digits.
Now, 11111111111111111111111111111110 is some negative number i.e. it is the Two's Compliment of some positive number. Let's go reverse.
First I'll subtract 1 from it. I get 11111111111111111111111111111101.
Now I will flip all digits, I get 00000000000000000000000000000010. This means TWO. 11111111111111111111111111111110 is negative representation of 2. It is -2.