bitwise or operator
Jan 3, 2014 at 12:43pm UTC
int main(void)
{
int a=0000000000000001;
int b=0000000000000010;
int c=a|b;
printf("%d",c);
while(!kbhit());
return 0;
}
ans should be 3 but it gives 9 why ?
Jan 3, 2014 at 12:47pm UTC
because when you begin a number with a zero, you're using an octal integer literal. The literal 010 is 8 in octal
and 8 | 1 gives 9
Jan 3, 2014 at 1:09pm UTC
in addition to Cubbi's answer, you cannot represent a binary number like you did, as I know.
Instead, I suggest you to use the std::bitset class.
1 2 3 4 5 6 7
#include <bitset>
#include <iostream>
std::bitset<16> a("0000000000000001" );
std::bitset<16> b("0000000000000010" );
std::bitset<16> c = a | b;
std::cout << c.to_ulong();
will print 3.
Topic archived. No new replies allowed.