开发者

Thinking in C++ shift operators

开发者 https://www.devze.com 2023-04-12 23:10 出处:网络
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 wh

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 g开发者_如何转开发etval; 
    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


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.


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消