简体   繁体   中英

Bitwise operators - precision

Three questions that I seem to be stuck on:

  1. Why is the code x & ~077 better than this line of code x & 0177700 . Would it be because there is less loss of precision?

  2. Why is this code incorrect for setting bit 5 in a number? num = num + 0x20 Would it be because we need to use logical or, | , not the + ?

  3. Why is this code x & 0xFF better than this line of code (x << 24) >> 24 ? The right expression could result in sign extension which changes the original int. I am sure that is correct from examples I have done.

1, why is this code x & ~077 better than this line of code x & 0177700.

Because in the second one you are making assumptions on the length of the integer type of x

2, why is this code incorrect for setting bit 5 in a number? num = num + 0x20 Would it be because we need to use logical or, |, not the +?

yes! they are different operators.

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