Skip to content
codingtube

codingtube

Coding and Programming tutorials

  • javascript
  • React
  • ES6
  • React js
  • coding
  • ffmpeg
  • java
  • programming
  • information
  • coding
  • Privacy Policy
  • Twitter trends
  • Age Calculatore
  • Codingtube Community
  • YouTube Tags Generator
  • About
  • Toggle search form

Bitwise Operators in C

Posted on October 29, 2021October 29, 2021 By christo No Comments on Bitwise Operators in C

One of C’s powerful features is a set of bit manipulation operators. These permit the programmer to access and manipulate individual bits within a piece of data

Table of Contents

  • The various Bitwise Operators in C
  • Bitwise AND Operator
    • Table of AND Operator
    • Truth Table of AND operator
  • Bitwise XOR Operator
  • The showbits( ) Function
  • One’s Complement Operator in C
  • Right Shift Operator
  • Programme of Right Shift Operator
  • Left Shift Operator

The various Bitwise Operators in C

OperatorMeaning
~One’s complement
>>Right shift
<<Left shift
&Bitwise AND
!Bitwise OR
^Bitwise XOR(Exclusive OR)
The various Bitwise Operators in C

Bitwise AND Operator

This operator is represented as &. Remember it is different than &&, the logical AND operator. The & operator operates on two operands. While operating upon these two operands they are compared on a bit-by-bit basis. Hence both the operands must be of the same type (either char or int). The second operand is often called an AND mask. The & operator operates on a pair of bits to yield a resultant bit

Table of AND Operator

First bitSecond bitFirst bit & Second bit
000
010
100
111
Bitwise AND Operator

Truth Table of AND operator

&01
000
101

Programme To test whether a bit in a number is ON or OFF

main( ) 
{ 
int i = 65, j ; 
 printf ( "\nvalue of i = %d", i ) ; 
 j = i & 32 ; 
 if ( j == 0 ) 
 printf ( "\nand its fifth bit is off" ) ; 
 else 
 printf ( "\nand its fifth bit is on" ) ; 
 j = i & 64 ; 
 if ( j == 0 ) 
 printf ( "\nwhereas its sixth bit is off" ) ; 
 else 
 printf ( "\nwhereas its sixth bit is on" ) ; 
} 

Output of this programme

Value of i = 65
and its fifth bit is off
whereas its sixth bit is on

Bitwise OR Operator in C

Bitwise OR Operator in C is that operator which is represented as |. The rules that govern the value of the resulting bit obtained after ORing of two bits is shown in the truth table below

!01
001
111
Bitwise OR Operator in C

Example of OR operator in C

main( ) 
{ 
 int b = 50 ; 
 b = b ^ 12 ; 
 printf ( "\n%d", b ) ; /* this will print 62 */ 
 b = b ^ 12 ; 
 printf ( "\n%d", b ) ; /* this will print 50 */ 
} 

Bitwise XOR Operator

The XOR operator is represented as ^ and is also called an Exclusive OR Operator. The OR operator returns 1, when any one of the two bits or both the bits are 1, whereas XOR returns 1 only if one of the two bits is 1

The truth table for the XOR operator

^01
001
110
Bitwise XOR Operator

The showbits( ) Function

showbits ( int n ) 
{ 
 int i, k, andmask ; 
 
 for ( i = 15 ; i >= 0 ; i-- ) 
 { 
 andmask = 1 << i ; 
 k = n & andmask ; 
 k == 0 ? printf ( "0" ) : printf ( "1" ) ; 
 } 
}

All that is being done in this function is using an AND operator and a variable andmask we are checking the status of individual bits. If the bit is OFF we print a 0 otherwise we print a 1.

These operators can operate upon ints and chars but not on floats and doubles.

One’s Complement Operator in C

On taking one’s complement of a number, all 1’s present in the number are changed to 0’s and all 0’s are changed to 1’s

Example

one’s complement of 1010 is 0101

one’s complement operator in action

main( ) 
{ 
 int j, k ; 
 for ( j = 0 ; j <= 3 ; j++ ) 
 { 
 printf ( "\nDecimal %d is same as binary ", j ) ; 
 showbits ( j ) ; 
 k = ~j ; 
 printf ( "\nOne’s complement of %d is ", j ) ;
showbits ( k ) ; 
 } 
} 

Output of this programme

Decimal 0 is same as binary 0000000000000000
One’s complement of 0 is 1111111111111111
Decimal 1 is same as binary 0000000000000001
One’s complement of 1 is 1111111111111110
Decimal 2 is same as binary 0000000000000010
One’s complement of 2 is 1111111111111101
Decimal 3 is same as binary 0000000000000011
One’s complement of 3 is 1111111111111100

Right Shift Operator

The right shift operator is represented by >>. It needs two operands. It shifts each bit in its left operand to the right. The number of places the bits are shifted depends on the number following the operator (i.e. its right operand)

Thus, ch >> 3 would shift all bits in ch three places to the right. 
Similarly, ch >> 5 would shift all bits 5 places to the right.

Programme of Right Shift Operator

main( ) 
{
int i = 5225, j, k ; 
 printf ( "\nDecimal %d is same as binary ", i ) ; 
 showbits ( i ) ; 
 for ( j = 0 ; j <= 5 ; j++ ) 
 { 
 k = i >>j ; 
 printf ( "\n%d right shift %d gives ", i, j ) ; 
 showbits ( k ) ; 
 } 
}

Output of this programme

Decimal 5225 is same as binary 0001010001101001
5225 right shift 0 gives 0001010001101001
5225 right shift 1 gives 0000101000110100
5225 right shift 2 gives 0000010100011010
5225 right shift 3 gives 0000001010001101
5225 right shift 4 gives 0000000101000110
5225 right shift 5 gives 0000000010100011

Left Shift Operator

This is similar to the right shift operator, the only difference being that the bits are shifted to the left, and for each bit shifted, a 0 is added to the right of the number

Example of Left Shift operator

main( ) 
{ 
 int i = 5225, j, k ; 
 printf ( "\nDecimal %d is same as ", i ) ; 
 showbits ( i ) ; 
 for ( j = 0 ; j <= 4 ; j++ ) 
 { 
 k = i <<j ; 
 printf ( "\n%d left shift %d gives ", i, j ) ; 
 showbits ( k ) ; 
 } 
} 

Output of this programme

Decimal 5225 is same as binary 0001010001101001
5225 left shift 0 gives 0001010001101001
5225 left shift 1 gives 0010100011010010
5225 left shift 2 gives 0101000110100100
5225 left shift 3 gives 1010001101001000
5225 left shift 4 gives 0100011010010000

c language Tags:Bitwise Operators in C, c language

Post navigation

Previous Post: Alternative of deprecated startActivityForResult
Next Post: Android to get the list of directory | Full project

Related Posts

Reading from a File in C c language
Loops in c – While loop – For loop c language
C Under Windows c language
C program Receives strings from keyboard and writes them to file c language
return statement in C c language
Hierarchy of Operations in C c language

Leave a Reply Cancel reply

You must be logged in to post a comment.

Recent Posts

  • Affiliate Marketing Principles
  • The Basics You Need to Know About Affiliate Marketing
  • Affiliate Marketing Options
  • All About Affiliate Marketing
  • Classification of Database Management Systems
  • Three-Tier and n-Tier Architectures
    for Web Applications
  • Two-Tier Client/Server Architectures for DBMSs
  • Basic Client/Server Architectures in DBMS
  • Centralized DBMSs Architecture in DBMS
  • Tools, Application Environments, and Communications Facilities in DBMS

Categories

  • Affiliate marketing (5)
  • Algorithm (43)
  • amp (3)
  • android (223)
  • Android App (8)
  • Android app review (4)
  • android tutorial (60)
  • Artificial intelligence (61)
  • AWS (3)
  • bitcoin (8)
  • blockchain (1)
  • c (5)
  • c language (105)
  • cloud computing (4)
  • coding (4)
  • coding app (4)
  • complex number (1)
  • Computer Graphics (66)
  • data compression (65)
  • data structure (188)
  • DBMS (44)
  • digital marketing (9)
  • distributed systems (11)
  • ffmpeg (26)
  • game (3)
  • html (6)
  • image processing (35)
  • Inequalities (1)
  • information (4)
  • java (212)
  • java network (1)
  • javascript (9)
  • kotlin (4)
  • leetcode (1)
  • math (21)
  • maven (1)
  • mysql (1)
  • Node.js (8)
  • operating system (109)
  • php (310)
  • Principle Of Mathematical Induction (1)
  • programming (6)
  • Python (4)
  • Python data structure (9)
  • React native (1)
  • React.js (22)
  • Redux (1)
  • seo (4)
  • set (12)
  • trigonometry (6)
  • vue.js (35)
  • XML (3)

sitemap

sitemap of videos

sitemap of webstories

sitemap of website

  • Affiliate marketing
  • Algorithm
  • amp
  • android
  • Android App
  • Android app review
  • android tutorial
  • Artificial intelligence
  • AWS
  • bitcoin
  • blockchain
  • c
  • c language
  • cloud computing
  • coding
  • coding app
  • complex number
  • Computer Graphics
  • data compression
  • data structure
  • DBMS
  • digital marketing
  • distributed systems
  • ffmpeg
  • game
  • html
  • image processing
  • Inequalities
  • information
  • java
  • java network
  • javascript
  • kotlin
  • leetcode
  • math
  • maven
  • mysql
  • Node.js
  • operating system
  • php
  • Principle Of Mathematical Induction
  • programming
  • Python
  • Python data structure
  • React native
  • React.js
  • Redux
  • seo
  • set
  • trigonometry
  • vue.js
  • XML
  • Blog
  • Data compression tutorial - codingpoint
  • How to change mbstring in php 5.6
  • How to diagnose out of memory killed PHP-FPM
  • Introduction to jQuery
  • Privacy
  • Affiliate marketing
  • Algorithm
  • amp
  • android
  • Android App
  • Android app review
  • android tutorial
  • Artificial intelligence
  • AWS
  • bitcoin
  • blockchain
  • c
  • c language
  • cloud computing
  • coding
  • coding app
  • complex number
  • Computer Graphics
  • data compression
  • data structure
  • DBMS
  • digital marketing
  • distributed systems
  • ffmpeg
  • game
  • html
  • image processing
  • Inequalities
  • information
  • java
  • java network
  • javascript
  • kotlin
  • leetcode
  • math
  • maven
  • mysql
  • Node.js
  • operating system
  • php
  • Principle Of Mathematical Induction
  • programming
  • Python
  • Python data structure
  • React native
  • React.js
  • Redux
  • seo
  • set
  • trigonometry
  • vue.js
  • XML
  • Blog
  • Data compression tutorial - codingpoint
  • How to change mbstring in php 5.6
  • How to diagnose out of memory killed PHP-FPM
  • Introduction to jQuery
  • Privacy

© codingtube.tech