C++ 之and, and_eq, or, or_eq, xor, xor_eq, not, not_eq操作符

本文介绍C++中较少被注意的操作符如and、and_eq、or、or_eq、xor、xor_eq、not、not_eq、bitand、bitor等,并通过实例展示了它们的应用方式。这些操作符在逻辑判断和位操作上提供了简洁的语法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在C++ 有一些不太受关注的关键字,这些关键字也是操作符:

and, and_eq, or, or_eq, xor, xor_eq, not, not_eq, bitand, bitor

and

表示逻辑与操作, 是C++原有操作符 && 的等效替换

bool success1 = execute_some_task();
bool success2 = execute_another_task();
if(success1 and success2)
{
	std::cout << "two tasks have been done successfully" << std::endl;
}

and_eq

表示相与并赋值,等价于 &= 操作符

unsigned char a = 0xFE;
unsigned char b = 0x02;
b and_eq a; // 等价于 b &= a;
std::cout << "new b is " << b << std::endl;

or

逻辑或, 等价于 || 操作符

int year = 2017;
//等价于 bool result = a % 400 == 0 || a % 400 != 0 && a % 4 == 0; 
bool result = a % 400 == 0 or a % 400 != 0 and a % 4 == 0;  

or_eq

相或并赋值,等价于 |= 操作符

unsigned short port = 0x0000;
port or_eq 0xFFEE; //等价于 port |= 0xFFEE;

xor

逻辑异或, 登记于 ^

int a = 0x5A xor 0xCE; //等价于 int a = 0x5A ^ 0xCE;

xor_eq

相异或并赋值

int a = 10;
int b = 15;
a xor_eq b xor_eq a xor_eq b; //等价于 a ^= b ^= a ^=b; 

not

逻辑非, 等价于操作符 !

bool islast = true;
if(not isLast)  //等价于 if(!isLast)
{
	// todo
}

bitand, bitor

与and,or类似,不过是按位操作, bitand等价于& , bitor 等价于 |

Product Description The author uses practical, concise code examples to illuminate a useful programming stratagem or warn against a dangerous practice. Readers will come away with a better understanding of how C++ is used in the real world. From the Inside Flap In the hands of an expert, C++ helps designers and programmers build systems that are modular, maintainable, and fast. To the novice, however, the size of the language can be intimidating. There are a lot of features in C++ and it takes some experience to learn which ones are appropriate for any situation. This book is intended to enhance and expedite that learning process. Most successful C++ programmers cannot recite chapter and verse from the language rules; instead, they have acquired a set of idioms and techniques that have worked well for them. Our goal is to help the C++ novice learn those idioms that have been most useful in practice. We also point out some of the most common pitfalls. We do not try to cover the entire language and we leave the ultra-precise definitions of language semantics to the reference manuals. Instead, we concentrate on helping the reader build programs that can be understood by someone who is not a C++ language lawyer. We not only discuss techniques for making programs elegant and fast; we also show how to make them easier to understand and maintain. Acknowledgements Almost none of the ideas and programming idioms in this book are my invention. My goal has been to present, in a way that allows novice C++ programmers to learn them quickly, what I consider to be the most important strategies and tactics I have learned from others in the eight years I have been using C++. Some of these lessons were learned by studying actual development projects as they moved from C to C++; others came from discussions with talented individuals. Many of the best ideas on templates and library design, including the ideas behind many of the container classes in this book, came from classes in the USL Standard Components that were originally designed by Martin Carroll, Andrew Koenig, and Jonathan Shopiro. I claim exclusive ownership of any errors in my versions. Andrew Koenig was a valuable resource as the local C++ language lawyer. The participants in the "C++ Strategies and Tactics" seminars I presented at several conferences helped inspire this book and refine its ideas. Other important ideas came from Tom Cargill, John Carolan, Jim Coplien, Mark Linton, Gerald Schwarz, and of course Bjarne Stroustrup, who also invented the C++ programming language that made the book possible in the first place. Brian Kernighan read several drafts of this book, and his excellent feedback has been a lot of help. I would also like to thank David Annatone, Steve Buroff, Tom Cargill, Bill Hopkins, Cay Horstman, Lorraine Juhl, Peter Juhl, Stan Lippman, Dennis Mancl, Scott Meyers, Barbara Moo, Lorraine Weisbrot Murray, Bjarne Stroustrup, Clovis Tondo, Steve Vinoski, and Christopher Van Wyk for their comments on early drafts of this book. Lorraine Weisbrot Murray also contributed the encouragement, understanding, support, and love that helped make the entire effort feasible. Rob Murray
<think>好的,我现在需要回答用户关于C++中的“and_eq”是什么意思的问题。首先,我得回想一下自己对这个关键字的了解。记得在C++中,有一些运算符的替代表示,它们是用关键字的形式来替代符号的,这些是所谓的“替代令牌”(alternative tokens)。这些替代令牌是为了方便在键盘上缺少某些符号的情况下使用,或者在代码可读性上有帮助。 首先,用户问的是“and_eq”,我应该先确认这个关键字对应的运算符是什么。根据C++的标准,这些替代令牌定义在<iso646.h>头文件中,或者在C++中可以通过包含<ciso646>来使用,不过大多数现代编译器可能已经默认支持,不需要显式包含。 接下来,“and_eq”对应的运算符应该是“&=”,也就是按位与并赋值操作符。这个运算符的作用是对变量进行按位与运算后赋值,比如x &= y相当于x = x & y。这里,“&”是按位与运算符,而“and”本身也是一个替代令牌,对应逻辑与运算符“&&”,但“and_eq”则是对应的复合赋值运算符。 我需要验证一下是否正确,避免混淆。例如,逻辑与是“&&”,替代令牌是“and”,而按位与是“&”,对应的复合赋值是“&=”,替代令牌是“and_eq”。所以用户提到的“and_eq”就是“&=”。 然后,我应该考虑如何向用户解释清楚,可能需要举一个例子。例如: int a = 5; // 二进制0101 a and_eq 3; // 3的二进制是0011 // 执行后,a的值变为0101 & 0011 = 0001,即1 这样用户就能明白实际效果了。同时,需要提醒用户注意,“and_eq”这样的替代令牌可能不常见,很多程序员更习惯使用“&=”,但了解这些替代形式有助于阅读旧代码或者特定编码风格的代码。 另外,可能需要提到历史背景,这些替代令牌是为了兼容一些字符集不支持标准运算符的情况,比如早期的键盘或字符编码。现在虽然大部分情况下直接使用符号运算符更常见,但了解这些关键字仍然有意义。 最后,确保回答的结构清晰,分点说明,并给出示例,这样用户能逐步理解。同时检查是否有其他相关的替代令牌,比如or_eq对应|=,xor_eq对应^=等,但用户的问题集中在and_eq,所以可以简要提及,但重点放在解释and_eq上。</think>在C++中,`and_eq` 是一个运算符的替代表示,属于C++标准定义的**替代令牌**(Alternative Tokens)。以下是逐步解释: --- ### 1. **基本定义** `and_eq` 对应的是复合赋值运算符 **`&=`**,即“按位与并赋值”。 - **符号形式**:`x &= y` - **替代形式**:`x and_eq y` 两者完全等价,作用是对变量进行按位与运算后赋值。 --- ### 2. **具体行为** 假设变量 `x` 和 `y` 是整数: ```cpp x and_eq y; // 等价于 x = x & y; ``` - `x & y` 会对 `x` 和 `y` 的二进制位进行按位与操作。 - 结果会赋值给 `x`。 --- ### 3. **示例** ```cpp #include <iostream> using namespace std; int main() { int a = 0b1100; // 二进制 1100(十进制 12) int b = 0b1010; // 二进制 1010(十进制 10) a and_eq b; // 等价于 a &= b; // 按位与结果:1100 & 1010 = 1000(十进制 8) cout << a; // 输出 8 return 0; } ``` --- ### 4. **替代令牌的来源** - C++标准通过头文件 `<ciso646>` 定义这些替代令牌(如 `and`, `or`, `not`, `and_eq` 等)。 - 现代编译器通常无需显式包含该头文件即可使用这些关键字。 --- ### 5. **实际应用场景** - **兼容性**:早期某些键盘缺少符号(如 `&`)时,可用 `and_eq` 替代。 - **代码风格**:部分开发者认为 `and_eq` 更易读(但实践中符号形式 `&=` 更常见)。 --- ### 6. **注意事项** - 不要与逻辑与运算符 `&&`(替代令牌为 `and`)混淆。 - `and_eq` 仅用于按位与操作,而 `&&` 用于布尔逻辑判断。 --- ### 总结 `and_eq` 是 `&=` 的替代写法,用于按位与并赋值操作。虽然实际编码中更常用 `&=`,但了解替代令牌有助于阅读旧代码或特殊编码风格的代码。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值