Check out example codes for "bitwise operator". It will help you in understanding the concepts better.

Code Example 1

``````12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bitwise XOR Operation of 12 and 25
00001100
^ 00011001
________
00010101  = 21 (In decimal)``````

Code Example 2

``````// C Program to demonstrate use of bitwise operators
#include <stdio.h>
int main()
{
// a = 5(00000101), b = 9(00001001)
unsigned char a = 5, b = 9;

// The result is 00000001
printf("a = %d, b = %d\n", a, b);
printf("a&b = %d\n", a & b);

// The result is 00001101
printf("a|b = %d\n", a | b);

// The result is 00001100
printf("a^b = %d\n", a ^ b);

// The result is 11111010
printf("~a = %d\n", a = ~a);

// The result is 00010010
printf("b<<1 = %d\n", b << 1);

// The result is 00000100
printf("b>>1 = %d\n", b >> 1);

return 0;
}``````

Code Example 3

``````#include <stdio.h>
int main()
{
printf("Output = %d\n",~35);
printf("Output = %d\n",~-12);
return 0;
}``````

Code Example 4

``````12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bit Operation of 12 and 25
00001100
& 00011001
________
00001000  = 8 (In decimal)``````

Code Example 5

``````bitwise complement of N = ~N (represented in 2's complement form)
2'complement of ~N= -(~(~N)+1) = -(N+1)``````

Code Example 6

``````#include <stdio.h>
int main()
{
int a = 12, b = 25;
printf("Output = %d", a&b);
return 0;
}``````

Code Example 7

``````Decimal         Binary           2's complement
0            00000000           -(11111111+1) = -00000000 = -0(decimal)
1            00000001           -(11111110+1) = -11111111 = -256(decimal)
12           00001100           -(11110011+1) = -11110100 = -244(decimal)
220          11011100           -(00100011+1) = -00100100 = -36(decimal)

Note: Overflow is ignored while computing 2's complement.``````

Code Example 8

``````35 = 00100011 (In Binary)

Bitwise complement Operation of 35
~ 00100011
________
11011100  = 220 (In decimal)``````

Learn ReactJs, React Native from akashmittal.com