Q:

C program to set/clear (high/low) bits of a number

0

C program to set/clear (high/low) bits of a number.

This program will set or clear (high or low) bits of a number, this operation can be performed using Bitwise OR (|) and Bitwise AND (&) operators.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

High/Low (Set/Clear) bits of a number using C program

/* C program to set and clear bits of a number.*/

#include <stdio.h>

int main()
{

    unsigned int num = 0x0C;

    /*set 0th and 1st bits*/
    num |= (1 << 0); /*set 0th bit*/
    num |= (1 << 1); /*set 1st bit*/

    printf("\nValue of num = %04X after setting 0th and 1st bits.", num);

    /*clear 0th and 1st bits*/
    num &= ~(1 << 0); /*set 0th bit*/
    num &= ~(1 << 1); /*set 1st bit*/

    printf("\nValue of num = %04X after clearing 0th and 1st bits.", num);

    return 0;
}

Output:

    Value of num = 000F after setting 0th and 1st bits.
    Value of num = 000C after clearing 0th and 1st bits.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C solved programs/examples on Bitwise Operators

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to swap two numbers using bitwise XOR op... >>
<< C program to demonstrate example of right shift (&...