c语言 宏定义 去除宏定义_定义宏以将C中的第N位设置为零

c语言 宏定义 去除宏定义

Macro definition:

宏定义:

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

Here,

这里,

  • SETPIN is the Macro name

    SETPIN是宏名称

  • PIN is the value whose bit to set to zero

    PIN是其位设置为零的值

  • N is the bit number

    N是位数

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.

    在此示例中,最初val的值为0xFF ,二进制值为“ 1111 1111”

  • 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.

    为了将第2位(即第三位)设置为零,当我们调用宏SETPIN(val,bit)时 ,第2位将设置为0。因此,输出将为“ 1111 1011”,即十六进制为0xFB

翻译自: https://www.includehelp.com/c-programs/define-a-macro-to-set-nth-bit-to-zero-in-c.aspx

c语言 宏定义 去除宏定义

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值