简体   繁体   中英

Bitwise operations between a bigger and a smaller number

int main(){
 long a = -246;
 int b = -5;
 int c = a | b;
}

Above code will have this bit pattern:

a = 1111111111111111111111111111111111111111111111111111111100001010
b =                                 11111111111111111111111111111011
c =                                 11111111111111111111111111111011

So if operating between two different bit counts, the smaller bit count is chosen and the remaining bits are discarded? Ie 0s don't get put to the left of b to match the bit-count of a ?

Integer promotion takes place here

long a = -246;
int b = -5;
int c = (int)( a | ((long)b) ); // <- equivalent to int c = a | b;

Note: as @Fredrik mentioned, if long is wider than int.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM