Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bitwise operations in c++, how to actually use it?

I understand

  11001110
& 10011000
= 10001000

but I want to test it myself

so I declare unsigned char and print it out, but it just gives me blank.

unsigned char result;
result= 11001110 & 10011000;

cout<<result;

Also tested with unsigned int, but it gives me 0, which is not what I expected

like image 604
Alston Avatar asked Mar 24 '23 23:03

Alston


1 Answers

11001110 and 10011000 aren't binary numbers (at least in the compiler's mind).

Binary 11001110 is 206, and 10011000 is 152, so you actually want (I think):

result = 206 & 152;

or use a std::bitset and then print result as a binary.

like image 125
Luchian Grigore Avatar answered Apr 10 '23 19:04

Luchian Grigore