In implementation , when we need to manipulate the data on binary representation, then go for bitwise operators when we’re working with bitwise operators, always modification will happens directly on binary format only. Bitwise operators need to be applied for an integral data type only. ie. we can’t apply for float double and long double type. Bitwise operators are ~ - its compliment << - bitwise left shift >> - bitwise right shift & - bitwise AND ^ - bitwise XOR | - bitwise OR
This operator returns compliment value of given i/p i.e, all is will be converted into dl format, all zeros will converted into is format when we’re working with any bitwise operators we require to take complete to 16 bit representation int a; a=5; 5 0000 0000 0000 010 o/p: 6 ~5-1111 1111 1111 1010 6535 1111 1111 1111 1111 ~5 1111 1111 1111 1010 22 20 65535 65530 -32768 5 32767 +32762(n-1) 65530 32763 -6 a=~61; 61 0000 0000 0011 1101 ~611111 1111 1100 0010(-1-32-16-8-4-1 655351111 1111 1111 1111 ~62 1111 1111 1100 0010 65535 65474 -32768 61 32767 32706 ---------- --------- ------------ 65474 32707 -62 a= ~127 0000 0000 0111 1111 o/p1- >28 1111 1111 1000 0000 (-1-64-32-16-8- 4-2-1) -128 a=~0 0000 0000 0000 0000 o/p -1 1111 1111 1111 1111(-1) a=~32767 0111 1111 1111 1111 o/p:-32768 1000 0000 0000 0000 a=~-15; -1 1111 1111 1111 1111 -15 1111 1111 1111 0001 ~-15 0000 0000 0000 1110(-14) a=~-255 o/p : 254 1111 1111 1111 1111 -255 1111 1111 0000 0001(-1-128-64-32- 16-8-4-2) 0000 0000 1111 1110(-254) (128+64+32+16+8+4+2) a=~-1 o/p : 0 a=~-32768 -32768- 1000 0000 0000 0000 o.p:32767 ~327680111 1111 1111 1111 3270 unsigned int u
o/p: 65530 ~5-1111 1111 1111 1010 65530
32767 0111 1111 1111 1111 ~32767 1000 0000 0000 0000 32768 There is no any format specifies are there which can represent binary data on console 1’s complement value is not equal to negation of given i/p Always 2’s compliment value is equal to negation of given i/p data 2’s compliment = 1’compliement +1
15 0000 0000 0000 1111 ~15 - 111 1111 1111 0000 1 0000 0000 0000 0001 1111 1111 1111 0001 (-1-8-4-2)(-15)
IN left shift operator, towards from the left side,’ n’ no. Of bits need to be dropped and towards from then right side empty places required to till with zeros. When we are dropping the bite towards from the left side then all 1’s will be shifted towards left side by ’n’ places then automatically value will increase. int a;
10.a= 32767<<75;-32768. 32767 0111 1111 1111 1111 32767<15 1000 0000 0000 0000(-32768)
4<<1 -5 1111 1111 1111 1011 4 0000 0000 0000 0100
-4<<2 ~4(-5) - 1111 1111 1111 1011
~4<<3; -5<<1 ; 1111 1111 1111 0110 (-1-1-8)-10 -5<<2; 1111 1111 1111 1100(-1-16-2) -20 -5<<3; 1111 1111 1101 1000(-1-1-2-4-32-40) Value =x<<n; x*2n Consider the range when using the formulae For any +ve integer left shift 16 makes the value as ‘0’ because +ve data binary representation is available in 16bits towards from left side. a=1<<16; 0 a= 32767 <<16; 0 a= 12345 << 16; 0
In right shift operator, towards from right side, ‘n’’ no. of bits need to be dropped and towards from left side empty places need to be filled with zeros for +ve data only. In right shift operator for negative no’s empty places need to be filled ones, so sign bit will not be modified, we will get negative data only. In right shift operator when we are dropping the towards from right side then all ones will be shift towards right side by ‘n’ places so value will be decreased. int a;
10>>1 0000 0000 0000 0101; 5
For any +ve no.of right shift 15 makes the values as zero because =ve data representation is available in 15 bits towards from right side.
~9>>1;
~9>>4 ; -10 1111 1111 1111 0110 9 0000 0000 0000 1001 ~9 (-10) 1111 1111 1111 0110 -10>>1 1111 1111 1111 1011 à-5(-1-4) -10>>2 1111 1111 1111 1101 (-1-2)-3 -10>>3 1111 1111 1111 1110(-1-1)-2
a | b | a&b | a/b | a^b |
1 | 1 | 1 | 1 | 0 |
0 | 1 | 1 | 0 | 1 |
1 | 0 | 1 | 0 | 1 |
0 | 0 | 0 | 0 | 0 |
20à 0000 0000 0000 0010 30 à 0000 0000 0000 0011 2&3 0000 0000 0000 0010 2 2/3 0000 0000 0000 0011 3 2^3 0000 0000 0000 0001 1 O/p: 2 3 1
20 0000 0000 0001 0100 30 0000 0000 0001 1110 30&20 0000 0000 0001 0100 20 30/20 0000 0000 0001 1110 30 30^20 0000 0000 0000 1010 10
O/p :-0 -1 -1.
O/p: 0-1 -1
O/p: 0-1 -1.
{ int a; a=10,20,30; printf(“a=%d”,a); } O/p: 10 Assignment operator having highest priority operators dts why first value will be passed to a variable.
{ int a; a=(10,20,30); printf(“a=%d”,a); } O/p: a=30
{ int a; a=10; if(a=0,12) printf (“welcome %d”,a); else printf (“Hello %d”,a); } O/p: welcome 0
{ int a; a=0; if(a=10,20) printf(“Welcome %d”,a); else printf(“Hello %d “,a);
if (a=10 ,2,0) if (a=2,0) if(10,2,0) L àR if(0) false O/p: Hello 10
{ int i; i=0; switch (i=1,2,3) { case 1: printf(“A%d”,i); break; case2: printf(“B%d”,i); break; case3 : printf(“C%d”,i); break; default: printf(“D %d “,i); O/p : c1
{ int a; a=1; switch (a= printf(“Bye”),printf(“ Hi”) { case1: printf(“%d”,a); break; case2: printf(“B%d”,a); break; case3:printf(“C%d”,a); break; default: printf(“D%d”,a); } O/p:ByeHiB3 switch (a= printf(“bye”),printf(“Hi”)) switch a=5,2) switch (3,2) switch (2) case2:B3
{ int a,b; for(a=1,b=5;a<=10,b<=8;a++,b++) printf (“\n%d”%d “,a,b); printf(“\na=%d b=%d”,a+10,b+10); } O/p : 1 5 2 6 3 7 4 8 15 b=19 When we are placing more than one expression as a condition in for loop, then finally evaluated expression will consider as condition.
int a,b,c; a=b=1; c=++a>1||++b>1; printf(“a=%d b=%d c=%d,a,b,c); } O/p: 2 1 1 When we are working with logical or operator if any one of the expression is true ,then return value is 1, dots why when the left side expression is true, that’s why right side part will not be evaluated.
{ int a, b, c; a=1;b=3; c=++a <1|| b<3; printf(“a=%d b=%d c=%d”,a,b,c); } O/p: 2 2 1
{ int a,b,c; a=2;b=4; c=a-- <1||--b>4; printf(“a=%d b=%d c=%d “,a ,b,c); } O/p :a=1 b=3 c=0
{ int a,b,c; a=1;b=2 c=++ a>2&&--b<<; printf(“a=%d b=%d c=%d ,a,b,c); } O/p: a=2 b=2 c=0 When we are working with logical AND operator both expressions or all expression are true then only return value is true i.e. ‘1’ that’s why when the left side expression is false ,rigt side will not be evaluated.
{ inta,b,c; a=1;b=3; c=++a>1&&--b<2; printf(“a=%d b=%d c=%d ,a,b,c); } O/p: 3 3 1
{ int a,b,c,d; a=1;b=2;c=3; d= ++a>1|| ++b>2 && --c<3; printf(“%d %d %d %d “, a,b,c,d); } O/p: a=2 b=2 c=3 d=1 When we are working with uniary operator with the combination of binary then highest priority will be given for unary operator.
{ int a,b,c,d; a=2;b=4;c=6; d=++a<2||--b>4&&--c<6; printf(“%d %d %d %d “,a,b,c,d); } O/p: 3 3 6 0
{ int a,b,c,d; a=1;b=3;c=5; d= ++a>2&& - -b<3|| ++c>5; printf(“%d %d %d %d “,a,b,c,d); } O/p: 2 3 5 1
You liked the article?
Like : 0
Vote for difficulty
Current difficulty (Avg): Medium
1/15
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.
Stay Updated
Get stories of change makers and innovators from the startup ecosystem in your inbox