Created
December 28, 2016 02:46
-
-
Save hygull/c090bd777f55c095f97a750370cab3f9 to your computer and use it in GitHub Desktop.
To use bitwise complement operator(with example) created by hygull - https://repl.it/Ex7l/2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Date of creation : 28/12/2016, Wednesday. | |
Aim of program : To use bitwise complement operator(with example). | |
Note : Computer stores -ve numbers as 2's complement of their +ve form. | |
eg. -2 will be stored as 2's complement of -2 | |
*/ | |
#include <stdio.h> | |
int main() | |
{ | |
int a; | |
printf("typeof(a) : int\n"); | |
printf("sizeof(a) : %d bytes\n", sizeof(a)); | |
a=1; | |
printf("if a = 1 then ~a = %d\n",~a); //-2 | |
/* | |
1 = 0000 0000 0000 0000 0000 0000 0000 0001 | |
~1 = 1111 1111 1111 1111 1111 1111 1111 1110 | |
Lets get the binary representation of -2 | |
2 = 0000 0000 0000 0000 0000 0000 0000 0010 (binary representation of 2) | |
~2 = 1111 1111 1111 1111 1111 1111 1111 1100 (1's complement of 2) | |
~2+1: | |
1111 1111 1111 1111 1111 1111 1111 1101 | |
+0000 0000 0000 0000 0000 0000 0000 0001 | |
------------------------------------------ | |
1111 1111 1111 1111 1111 1111 1111 1110 (Binary representation of -2 or 2's complement of 2) | |
Now you can compare that 2's complement of 2 and 1's complement of 1, both are same. | |
*/ | |
a=2; | |
printf("if a = 2 then ~a = %d\n",~a);//-3 | |
a=3; | |
printf("if a = 3 then ~a = %d\n",~a);//-4 | |
a=4; | |
printf("if a = 4 then ~a = %d\n",~a);//-5 | |
return 0; | |
} | |
/* OUTPUT:- | |
typeof(a) : int | |
sizeof(a) : 4 bytes | |
if a = 1 then ~a = -2 | |
if a = 2 then ~a = -3 | |
if a = 3 then ~a = -4 | |
if a = 4 then ~a = -5 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment