• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Operators in C

Explain Operators in C Language

Bitwise operators :-

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

 

1’s compliment : – (~) :

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;            57 0000           0000          0000          010

o/p: 6         ~5-71111            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  70000               0000          0011          1101

~6171111                 1111          1100          0010(-1-32-16-8-4-1

6553571111             1111          1111          1111

~62  71111               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      7    1111          1111          1111          1111

-15    7  1111          1111          1111          0001

~-15  7    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-7  1000          0000          0000   0000

o.p:32767           ~3276870111           1111          1111 1111 3270

 

unsigned int u

  1. u=~5 57 0000              0000          0000          0101

o/p: 65530 ~5-71111           1111          1111          1010  65530

 

  1. u=~32767; 32768

 

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

  • 2’scompliemt of 15 ;-15

15   7 0000    0000    0000    1111

~15 –7 111     1111   1111     0000

7 0000 0000 0000 0001

1111 1111 1111 0001 (-1-8-4-2)(-15)

 

 

Left shift operator(<<)

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;

  1. a=10<<1;20            10  7 0000 0000 0000 1010
  2. a=10<<2;40            10<<1 0000 0000 0001 0100
  3. a=10<<3;80             10<<2 0000 0000 0010 1010 (23+8)
  4. a=10<<4;160            10<<3 0000 0000 0101 0000(46+16)
  5. a=1<<1;2                       10<<4 0000 0000 1010 0000(128+32)
  6. a=2<<1;4                       0000 0000 0000 0001
  7. a=3<<1;6                      0000 0000 0000 0010
  8. a=100<1;200
  9. a=1<15;-32768 1000 0000 0000 0000 -23%

10.a= 32767<<75;-32768.

32767      0111      1111     1111         1111

32767<15 1000     0000      0000          0000(-32768)

  1. a=-5<<1;-10 -1 7 1111 1111 1111 1111

4<<1                             -5  7 1111 1111 1111 1011

4 7 0000  0000  0000 0100

  1. a=-5<<2;-20

-4<<2                             ~4(-5) –7 1111 1111 1111 1011

  1. a=-5<<3;-40

~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

 

Right shift Operator (>>):

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;

  1. a= 10>>1;5            10  7 0000  0000  0000  1010

10>>1 0000  0000  0000  0101; 5

  1. a=10>>2; 210>>2           0000 0000   0000   0010;2
  2. a= 10 >>3;1 10>>3  0000 0000  0000   0001;1
  3. a= 10>>4                            0000 0000 0000  0000;0
  4. a=1 >>1;0
  5. a= 2>>1;1
  6. a= 8>>1;4
  7. a=50>>1;55
  8. a=150>>1;75 32767  0111 1111 1111 1111
  9. a= 32767>>15;0 32767>>15 0000 0000 0000 0000
  10. a= 12345 >>15;0 Value=x>>n; x/2n

 

 

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.

  1. a= -10>>1;

~9>>1;

  1. a=-10>>2;-9>>2;
  2. a= -10>>3;9>>3;
  3. a=-10>>4;     -1 7 1111 1111 1111 1111

~9>>4 ;   -107 1111 1111 1111 0110

9 70000 0000 0000 1001

~9 (-10) 1111 1111 1111 0110

-10>>1  7 1111  1111 1111 1011  à-5(-1-4)

-10>>2 7  1111   1111  1111 1101 (-1-2)-3

-10>>3 7 1111   1111   1111  1110(-1-1)-2

 

aba&ba/ba^b
11110
01101
10101
00000
  • printf(“%d %d %d “, 2&3 ,2/3, 2^3);

 

20à 0000 0000 0000 0010

30 à 0000 0000 0000 0011

2&3 7 0000 0000 0000 0010         2

2/3   7 0000 0000 0000 0011         3

2^3   70000 0000 0000 0001         1

O/p: 2 3 1

  • printf(“%d %d %d”, 30&20 ,30/20 ,30^20);

20   7 0000  0000  0001 0100

30 7  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

 

  • printf(“%d %d %d “, 0&-1 ,0/-1,0^-1);
  • — 0000 0000 0000 0000

O/p  :-0  -1  -1.

  • printf(“%d %d %d “, a&-10,a/-10,a^-10);

O/p: 0-1 -1

 

  • printf(“%d %d %d “, 32767&-32768,32767/-32768, 32767^–32768);

O/p: 0-1 -1.

 

 

 

Void main()

{

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.

 

Void main()

{

int a;

a=(10,20,30);

printf(“a=%d”,a);

}

O/p: a=30

 

  • Void main()

{

int a;

a=10;

if(a=0,12)

printf (“welcome %d”,a);

else

printf (“Hello %d”,a);

}

O/p: welcome 0

 

 

 

void main()

{

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

 

 

Void main()

{

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

 

 

 

Void main()

{

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

 

 

void main()

{

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.

 

 

void main()

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.

 

 

void main()

{

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

 

 

 

void main()

{

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

 

 

Void main()

{

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.

 

void main()

{

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

 

 

 

 

void main()

{

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.

 

 

Void main()

{

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

 

 

 void main()

{

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

Summary
Review Date
Reviewed Item
Operators in C
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Operators in C"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.