C++ Bitwise Shift

renton79 101 Apr 03, 2010 at 21:18

I have a piece of code I can’t figure out how it works, there are two boolean vars true and false. As I don’t know C++ at all I have a bit of problem to figure out what happens when both of them are bitwise shifted by 1 and 2.

bool a = true; // in integer is equal to 1
bool b = false; // in integer is equal to 0

could anyone please tell me what would be the output trace of a and b shifted as below as I can’t check it myself

int c = (a << 1) (b << 2);

3 Replies

Please log in or register to post a reply.

Reedbeta 167 Apr 04, 2010 at 00:14

You are familiar with bitwise operations, aren’t you? In C/C++, << is left shift and | is bitwise OR. Given that, the answer should be obvious…

renton79 101 Apr 04, 2010 at 09:59

I got it all figured at the end, thanks anyway, sorry for posting so basic questions but I am rewriting some code from C++ and I am not familiar with code, I really have to take my time and learn the basics soon

Sol_HSA 119 Apr 05, 2010 at 20:01

I did this little boolean primer some time back, may be helpful: http://iki.fi/sol/boolean.html