GOEDUHUB Tutorials    ||    AI    ||    ML  ||    NLP    ||    OpenCV    ||    Python    ||    OOPS    ||    DBMS    ||    DSA    ||    JAVA    ||    Linux    ||    C Programming
7976731765 Go To Your University       ||       Placement Preparation       ||        Technical-MCQ and Interview Questions
Online Live Classes Youtube Live Link
0 like 0 dislike
27 views
in Tutorial & Interview questions by Goeduhub's Expert (8.1k points)

Bitwise operators can be used to perform bit level operation on variables. Below is a list of all six bitwise operators supported in C:

1 Answer

0 like 0 dislike
by Goeduhub's Expert (8.1k points)
 
Best answer

Bitwise operators can be used to perform bit level operation on variables. Below is a list of all six bitwise operators supported in C:

Symbol                     Operator

&           bitwise AND

|            bitwise inclusive OR

^            bitwise exclusive OR (XOR)

~           bitwise not (one's complement)

<<          logical left shift

>>          logical right shift 

Following program illustrates the use of all bitwise operators:

#include <stdio.h> 

int main(void)

  unsigned int a = 29;    /* 29 = 0001 1101 */     unsigned int b = 48;    /* 48 = 0011 0000 */   int c = 0;          

   c = a & b;              /* 32 = 0001 0000 */   printf("%d & %d = %d\n", a, b, c );

   c = a | b;              /* 61 = 0011 1101 */   printf("%d | %d = %d\n", a, b, c );

   c = a ^ b;              /* 45 = 0010 1101 */   printf("%d ^ %d = %d\n", a, b, c );

   c = ~a;                 /* -30 = 1110 0010 */   printf("~%d = %d\n", a, c );

   c = a << 2;             /* 116 = 0111 0100 */   printf("%d << 2 = %d\n", a, c );

   c = a >> 2;             /* 7 = 0000 0111 */   printf("%d >> 2 = %d\n", a, c );

   return 0; }

Bitwise operations with signed types should be avoided because the sign bit of such a bit representation has a particular meaning. Particular restrictions apply to the shift operators:

  • Left shifting a 1 bit into the signed bit is erroneous and leads to undefined behavior.
  • Right shifting a negative value (with sign bit 1) is implementation defined and therefore not portable.
  • If the value of the right operand of a shift operator is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.

Masking: 

Masking refers to the process of extracting the desired bits from (or transforming the desired bits in) a variable by using logical bitwise operations. The operand (a constant or variable) that is used to perform masking is called a mask.

Masking is used in many different ways: 

  • To decide the bit pattern of an integer variable.
  • To copy a portion of a given bit pattern to a new variable, while the remainder of the new variable is filled with 0s (using bitwise AND)
  • To copy a portion of a given bit pattern to a new variable, while the remainder of the new variable is filled with 1s (using bitwise OR).
  • To copy a portion of a given bit pattern to a new variable, while the remainder of the original bit pattern is inverted within the new variable (using bitwise exclusive OR).

#include <limits.h> 

void bit_pattern(int u) 

{    

int i, x, word;    

unsigned mask = 1;

 word = CHAR_BIT * sizeof(int);    

mask = mask << (word - 1);    /* shift 1 to the leftmost position */    

for(i = 1; i <= word; i++)    

{        

x = (u & mask) ? 1 : 0;  /* identify the bit */        

printf("%d", x);         /* print bit value */        

mask >>= 1;              /* shift mask to the right by 1 bit */    

}

Related questions

0 like 0 dislike
1 answer 18 views
0 like 0 dislike
1 answer 14 views
0 like 0 dislike
0 answers 20 views
0 like 0 dislike
1 answer 21 views
0 like 0 dislike
1 answer 22 views

 Goeduhub:

About Us | Contact Us || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
 Placements:   List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Important Lists: List of NITsList of IITsList of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG)College ReviewsCollege Fest, Events & WorkshopsKnowledge ShareTrainees/Interns After 15-04-2020
Exams & Cutoffs: JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs || Cutoff-GATECutoff_IIT-JEECS-ScopeECE ScopeEE-Scope
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
...