10、C语言运算符详解

C语言运算符详解

1. 位移操作与位掩码

在C语言中,位移操作可用于操作位掩码。例如,下面的 setBit() 函数用于设置位掩码 m 中指定位置 p 的位:

// Function setBit()
// Sets the bit at position p in the mask m.
// Uses CHAR_BIT, defined in limits.h, for the number of bits in a byte.
// Return value: The new mask with the bit set, or the original mask
//               if p is not a valid bit position.
unsigned int setBit( unsigned int mask, unsigned int p )
{
  if ( p >= CHAR_BIT * sizeof(int) )
    return mask;
  else
    return mask | (1 << p);
}

位移运算符的优先级低于算术运算符,但高于比较运算符和其他位运算符。所以,表达式 mask | (1 << p) 中的括号实际上并非必需,但能提高代码的可读性。

2. 内存寻址运算符

以下是用于数组元素寻址、结构体成员寻址以及使用指针访问对象和函数的五个运算符:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值