Home » C programming language

How to set, clear and toggle a single bit in C language?

Here, we are going to learn how do you set, clear and toggle a single bit of a number in C programming language?
Submitted by IncludeHelp, on February 14, 2019

Given a number and we have to 1) set a bit, 2) clear a bit and 3) toggle a bit.

1) Setting a bit

To set a particular bit of a number, we can use bitwise OR operator (|), it sets the bits – if the bit is not set, and if the bit is already set, bit’s status does not change.

Let suppose there is a number num and we want to set the Xth bit of it, then the following statement can be used to set Xth bit of num

    num |= 1 << x;

2) Clearing a bit

To clear a particular bit of a number, we can use bitwise AND operator (&), it clears the bit – if the bit is set, and if the bit is already cleared, bit’s status does not change.

Let suppose there is a number num and we want to clear the Xth bit of it, then the following statement can be used to clear Xth bit of num

    num &= ~(1 << x);

3) Toggling a bit

To toggle a particular bit of a number, we can use bitwise XOR operator (^), it toggles the bit – it changes the status of the bit if the bit is set – it will be un-set, and if the bit is un-set – it will be set.

Let suppose there is a number num and we want to toggle the Xth bit of it, then the following statement can be used to toggle Xth bit of num

    num ^= 1 << x;

Consider the following code – Here, we are performing all above mentioned operations

#include <stdio.h>

int main(){
    //declaring & initializing an 1 byte number
    /*
        num (hex) = 0xAA
        its binary = 1010 1010
        its decimal value = 170
    */
    unsigned char num = 0xAA; 
    printf("num = %02X\n", num);
    
    //setting 2nd bit 
    num |= (1<<2);
    //now value will be: 1010 1110 = 0xAE (HEX) = 174 (DEC)
    printf("num = %02X\n", num);

    //clearing 3rd bit 
    num &= ~(1<<3);
    //now value will be: 1010 0110 = 0xA6 (HEX) = 166 (DEC)
    printf("num = %02X\n", num);    
    
    //toggling bit 4th bit
    num ^= (1<<4);
    //now value will be: 1011 0110 = 0xB6 (HEX) = 182 (DEC)
    printf("num = %02X\n", num);

    //toggling bit 5th bit
    num ^= (1<<5);
    //now value will be: 1001 0110 = 0x96 (HEX) = 150 (DEC)
    printf("num = %02X\n", num);    
    
    return 0;    
}

Output

num = AA
num = AE
num = A6
num = B6
num = 96

Read more...






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.