Home » C programs » C programs on Bitwise Operators

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.

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;
    }

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






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.