设置和清除位的宏 [英] Macros to set and clear bits

查看:17
本文介绍了设置和清除位的宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一些简单的宏来简化设置和清除位的任务,这应该是一项简单的任务,但我似乎无法让它们正常工作.

Im trying to write a few simple macros to simplify the task of setting and clearing bits which should be a simple task however I cant seem to get them to work correctly.

#define SET_BIT(p,n) ((p) |= (1 << (n)))
#define CLR_BIT(p,n) ((p) &= (~(1) << (n)))

推荐答案

试试

#define CLR_BIT(p,n) ((p) &= ~((1) << (n)))

但是,由于一般宏邪恶的各种原因,我建议不要使用宏.使用内联函数并通过引用传递,如下所示:

However for various reasons of general macro evil I would advise not using a macro. Use an inline function and pass by reference, something like this:

static inline void set_bit(long *x, int bitNum) {
    *x |= (1L << bitNum);
}

这篇关于设置和清除位的宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆