0

I have two operation, and I am assuming both are doing ShiftLeft bitwise operation.

#define TACH_MAX_OWN_ORDERS 1<<6
int myVal = 1<<6
cout<<"Value after operation|"<<myVal <<"|"<<TACH_MAX_OWN_ORDERS<<endl;

output of TACH_MAX_OWN_ORDERS value always surprise me.

Value after operation|64|16

Do anyone have any clue, how it comes??? Thanks

1 Answers1

8

Macros replace text as is, so it will result in

cout<<"Value after operation|"<<myVal <<"|"<<1<<6<<endl;

the << won't result in (int)1<<6 but rather ([...] << 1) << 6 where [...] will have std::cout at the deepest level. This means your macro will always result in 16 when used in std::cout, because 1 and 6 are shifted into the out stream ("1" + "6") instead of the actual numerical value 64.


You should put parantheses around the statement to avoid this:

#define TACH_MAX_OWN_ORDERS (1<<6)

or even better, since you should avoid macros, if available try to use compile time constants:

constexpr int TACH_MAX_OWN_ORDERS = 1 << 6;
Stack Danny
  • 7,754
  • 2
  • 26
  • 55