Jump to content


C++ Bitwise Shift


3 replies to this topic

#1 renton79

    New Member

  • Members
  • Pip
  • 8 posts

Posted 03 April 2010 - 09:18 PM

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

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 5307 posts
  • LocationBellevue, WA

Posted 04 April 2010 - 12:14 AM

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...
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 renton79

    New Member

  • Members
  • Pip
  • 8 posts

Posted 04 April 2010 - 09:59 AM

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

#4 Sol_HSA

    Senior Member

  • Members
  • PipPipPipPip
  • 510 posts
  • LocationNowhere whenever

Posted 05 April 2010 - 08:01 PM

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users