用 C++ 思考轮班操作员

发布于 2024-12-10 02:45:03 字数 1280 浏览 1 评论 0 原文

我正在阅读一本关于 C++ 标准的书:Bruce Eckel 的《Thinking in C++》。

本书中对许多 C++ 功能进行了很好的解释,但我在某些方面遇到了障碍,例如,当我想编写游戏时,它是否对我有帮助,它的工作原理让我很恼火我真的无法从给出的解释中得到它。

我想知道这里是否有人可以帮助我解释这个示例程序的实际工作原理:

printBinary.h -

void printBinary(const unsigned char val); 

printBinary.cpp -

#include <iostream>

void printBinary(const unsigned char val) {
    for (int i = 7; i >= 0; i--) {
        if (val & ( 1 << i)) 
            std::cout << "1";
        else 
            std::cout << "0"; 
    }
}

Bitwise.cpp -

#include "printBinary.h" 
#include <iostream> 

using namespace std; 

#define PR(STR, EXPR) \ 
cout << STR; printBinary(EXPR); cout << endl; 

int main() {

    unsigned int getval; 
    unsigned char a, b; 
    cout << "Enter a number between 0 and 255: ";
    cin >> getval; a = getval; 
    PR ("a in binary: ", a);
    cin >> getval; b = getval; 
    PR ("b in binary: ", b); 
    PR("a | b = ", a | b);

这个程序应该向我解释移位按位运算符 (<<) 和(>>) 可以工作,但我根本不明白,我的意思是我确信我知道它如何使用 cin 和 cout 工作,但我不理解这一点是不是很愚蠢?

这篇文章比其他文章更让我困惑:

if (val & ( 1 << i))

感谢您的帮助

I'm reading through a book on C++ standards: "Thinking in C++" by Bruce Eckel.

A lot of the C++ features are explained really well in this book but I have come to a brick wall on something and whether it may or may not help me when I wish to program a game for example, it's irking me as to how it works and I really cannot get it from the explanation given.

I was wondering if anybody here could help me in explaining how this example program actually works:

printBinary.h -

void printBinary(const unsigned char val); 

printBinary.cpp -

#include <iostream>

void printBinary(const unsigned char val) {
    for (int i = 7; i >= 0; i--) {
        if (val & ( 1 << i)) 
            std::cout << "1";
        else 
            std::cout << "0"; 
    }
}

Bitwise.cpp -

#include "printBinary.h" 
#include <iostream> 

using namespace std; 

#define PR(STR, EXPR) \ 
cout << STR; printBinary(EXPR); cout << endl; 

int main() {

    unsigned int getval; 
    unsigned char a, b; 
    cout << "Enter a number between 0 and 255: ";
    cin >> getval; a = getval; 
    PR ("a in binary: ", a);
    cin >> getval; b = getval; 
    PR ("b in binary: ", b); 
    PR("a | b = ", a | b);

This program is supposed to explain to me how the shift bitwise operator (<<) and (>>) work but I simply don't get it, I mean sure I know how it works using cin and cout but am I stupid for not understanding this?

this piece in particular confuses me more so than the rest:

if (val & ( 1 << i))

Thanks for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

喜你已久 2024-12-17 02:45:03
if (val & ( 1 << i))

考虑以下二进制数 (128):

10000000

& 是按位“AND” - 0 & 0 = 0,0 & 1 = 1 & 1 0 = 0,1 & 1 = 1。

<< 是按位移位运算符;它将移位后的数字的二进制表示形式向左移动。

<代码>00000001 << 1 = 00000010; <代码>00000001 << 2 = 00000100。

在所有迭代中将其写在一张纸上,看看会产生什么结果。

if (val & ( 1 << i))

Consider the following binary number (128):

10000000

& is bitwise "AND" - 0 & 0 = 0, 0 & 1 = 1 & 0 = 0, 1 & 1 = 1.

<< is bitwise shift operator; it shifts the binary representation of the shifted number to left.

00000001 << 1 = 00000010; 00000001 << 2 = 00000100.

Write it down on a piece of paper in all iterations and see what comes out.

不必了 2024-12-17 02:45:03
1 << i

获取 1int 表示形式,并将其左移 i 位。

val & x

valx 之间的按位 AND(其中 x1 <<我在这个例子中)。

if(x)

测试 x 转换为 bool 是否为 true。转换为 bool 的整型类型的任何非零值均为 true。

1 << i

takes the int-representation of 1 and shifts it i bits to the left.

val & x

is a bit-wise AND between val and x (where x is 1 << i in this example).

if(x)

tests if x converted to bool is true. Any non-zero value of an integral type converted to bool is true.

初懵 2024-12-17 02:45:03

<<在您显示的代码中有两种不同的含义。

if (val & (1 << i))

<<用于位移位,因此值 1 会左移 i 位

cout << ....

Stream 类重载了运算符 <<,因此这里与之前的含义不同。
在这种情况下,<<是一个将其右边的内容输出到cout的函数

<< has two different meanings in the code you shown.

if (val & (1 << i))

<< is used to bitshift, so the value 1 will be shifted left by i bits

cout << ....

The stream class overloads the operator <<, so here it has a different meaning than before.
In this case, << is a function that outputs the contents on its right to cout

沫雨熙 2024-12-17 02:45:03
if (val & ( 1 << i))

这会检查第 i 个位置的位是否已设置。 (1 << i) 类似于 000001000(i = 3)。操作返回非零,这意味着 val 已设置相应的位。

if (val & ( 1 << i))

This checks if the bit in i-th position is set. (1 << i) is something like 000001000 for i = 3. Now if the & operation returns non-zero, that means val had the corresponding bit set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文