×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

How to set, clear, and toggle a 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? By IncludeHelp Last updated : April 20, 2023

Given a number and we have to:

  1. Set a bit
  2. Clear a bit
  3. Toggle a bit

1) How to Set a bit in C?

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) How to Clear a bit in C?

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) How to Toggle a bit in C?

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;

4) How to set, clear, and toggle a bit in C?

  • To set a particular bit of a number, we can use bitwise OR operator (|).
  • To clear a particular bit of a number, we can use bitwise AND operator (&).
  • To toggle a particular bit of a number, we can use bitwise XOR operator (^).

C program to set, clear, and toggle a bit

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



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.