C Foundation

What is C? C Compiler Installation C Extensions C Compiler C Interpreter C Program Structure

C Basics

C Keywords C Data Types C Identifiers C Variables C Constant C Escape Sequences C Constant and Volatile C Typecast

Operators

What is Operator C Comma Operator C Arithmetic Operators C Relational Operators C Logical Operators C Bitwise Operators C Conditional Operators C : : Operator C Operator Priority

Basic IO's

Basic IO's C Formatted Functions C Unformatted Functions C Common Functions

Control Statements

What is Control Statement C if Statement C if else Statement C Nested if Statement C Else if Statement C Break Statement C Continue Statement C Switch Statement C Goto Statement

Looping

What is Control Loop C for Loop C Nested for Loop C while Loop C Nested while Loop C do while Loop C Nested do while loop

Functions

What is Function C User Defined Functions C Recursion C Passing Parameters

Scope

Scope C Local Scope C Global Scope

Storage Classes

What is Storage Class C Auto C Extern C Static C Register

Array

What is Array C One Dimensional Array C Two Dimensional Array C Multi Dimensional Array C Arrays Of Strings

String

What is String C String Functions

Pointer

What is Pointer C Pointers Arithmetic C Pointer to Pointer C Pointers and Arrays C Pointers and Strings C Pointer to Functions Void Pointers Null Pointers C Null and Void Pointer

Structure

What is Structure C Struct within Struct C Array within Structure C Pointer to Structure C Structure and Function C Enum C Bitfield Structure C Type def

Union

What is Union

Files

What is File C read a file C write a file C File Handling C Error Handling C Low Level Disk I/O C Other file functions

Memory Allocation

What is Memory Allocation C Malloc() C Calloc() C Free() C Realloc() C Coreleft()

C Reference

All ASCII Code Basic C Questions

C Interview

C Interview Sets All Star Patterns All Number Patterns All Alphabet Patterns All Series Patterns
The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Bitwise Operators

We knew that, all integer variables represented internally as binary numbers. A value of type int consists of 32 binary digits, known to us as bits. We can operate on the bits that make up integer values using the bitwise operators.

Bitwise Operator's Facts

  • A bitwise operator which operates on each bit of data.
  • Bitwise operators only operates on integer operands such as int, char, short int, long int.
C Bitwise Operators

All Bitwise Operators

C provides 6 bitwise operators to operate on individual bits in an integer quantity.

Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
>> Right shift
<< Left shift
~ One's Complement

Bitwise AND Operator

Bitwise AND operator, &, combines corresponding bits in its tow operands such that if both bits are 1, the result is 1 otherwise the result is 0.

Bitwise AND Operator Table

Input Output
X Y Z
0 0 0
0 1 0
1 0 0
1 1 1

Bitwise AND Operator Program

Let us write a C program to demonstrate Bitwise AND operator

bitwiseand.c
#include <stdio.h>
int main()
{
int a = 10, b = 2;
printf("a & b = %d ", a & b);
return 0;
}
  • a & b = 2

Note:

Binary representation is given below.

BitwiseAND

Bitwise OR Operator

The bitwise OR operator, |, combines corresponding bits such that if either or both bits are 1, then the result is 1. Only if both bits are 0 is the result 0.

Bitwise OR Operator Table

Input Output
X Y Z
0 0 0
0 1 1
1 0 1
1 1 1

Bitwise OR Operator Program

Let us write a C program to demonstrate Bitwise OR operator

bitwiseor.c
#include <stdio.h>
int main()
{
int a = 10, b = 2;
printf("a | b = %d ", a | b);
return 0;
}
  • a | b = 10

Note:

Binary representation is given below.

BitwiseOR

Bitwise XOR Operator

Bitwise exclusive OR (XOR) operator, ^, combines corresponding bits such that if both bits are the same the result is 0; otherwise, the result is 1.

Bitwise XOR Table

Input Output
X Y Z
0 0 0
0 1 1
1 0 1
1 1 0

Bitwise XOR Program

Let us write a C program to demonstrate Bitwise XOR operator

bitwisexor.c
#include <stdio.h>
int main()
{
int a = 10, b = 2;
printf("a ^ b = %d ", a ^ b);
return 0;
}
  • a ^ b = 8

Note:

Binary representation is given below.

bitwiseEXOR

Right shift Operator (>>)

Right shift operator requires two operands. It takes Left hand side operand as bit sequence or bit Pattern to be shifted and right hand side operand as positive integer or unsigned integer that indicates the number of displacements or bit positions to the right.

C Program for Right Shift

Let us write a C program to demonstrate Right Shift operator

rightshift.c
#include <stdio.h>
int main()
{
int a = 8,b;
a>>= 1;
b = a;
printf("The Right shifted data of 8 by 1 = %d ",b);
return 0;
}
  • The Right shifted data of 8 by 1 = 4

Note:

Binary representation is given below.

RightShift

Left shift Operator (<<)

Left shift operator also requires two operands. It takes Left hand side operand as bit sequence or bit Pattern to be shifted and right hand side operand as positive integer or Unsigned integer that indicates the number of displacement or bit positions to the left.

C Program for Left Shift

leftshift.c
#include <stdio.h>
int main()
{
int a = 4, b;
a<<= 1;
b = a;
printf("The Left shifted data of 4 by 1 = %d ",b);
return 0;
}
  • The Left shifted data of 4 by 1 = 8

Note:

Binary representation is given below.

LeftShift

One's Complement

One's Complement operator is a unary operator, it always precedes to the variable or an operand. One's Complement operator inverts bits of its operand. So, 1s becomes 0s and 0s becomes 1s.

Table for One's Complement

Input Output
X Y
0 1
1 0

C Program for One's Complement

onescomplement.c
#include <stdio.h>
int main()
{
unsigned short int a = 10;
a = ~a;
printf("After One's Complement a = %u", a);
return 0;
}
  • After One's Complement a = 65525

Note:

If a variable 'a' is declared under normal int data type some garbage value will be displayed say -11. Binary representation is given below.

BinaryOnesComplement

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report