ADVERTISEMENT
ADVERTISEMENT

Define a Macro to set Nth bit to Zero in C

Here, we will learn how to set Nth bit of a number/PIN to Zero in C programming language by using Macro?
Submitted by IncludeHelp, on August 30, 2018

Macro definition:

 #define SETPIN(PIN,N) (PIN &= ~(1<<N))

Here,

  • SETPIN is the Macro name
  • PIN is the value whose bit to set to zero
  • N is the bit number

Example:

#include <stdio.h>

#define SETPIN(PIN,N) (PIN &= ~(1<<N))

int main(){
	
	unsigned char val = 0xFF;
	unsigned char bit = 2;
	
	printf("val = %X\n",val);
	
	//SET bit number 2 to zero 
	SETPIN(val,bit);
	printf("Aftre setting  bit %d to zero, val = %X\n", bit, val);
	
	return 0;	
}

Output

    val = FF
    Aftre setting  bit 2 to zero, val = FB

Explanation:

  • In this example, initially the value of val is 0xFF that is "1111 1111" in binary.
  • To set bit number 2 (i.e. third bit) to zero, when we call macro SETPIN(val, bit), the bit number 2 will set to 0. Thus, the output will be "1111 1011" i.e. 0xFB in Hexadecimal.

C Preprocessors Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

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.