Consider the following example:
The first case:
short x=255; x = (x<<8)>>8; cout<<x<<endl;
Second case:
short x=255; x = x<<8; x = x>>8; cout<<x<<endl;
The output in the first case is 255, while in the second case -1. -1 because the output makes sense since cpp performs an arithmetic shift to the right. Here are the intermediate x values ββto get -1 as output.
x: 0000 0000 1111 1111 x<<8:1111 1111 0000 0000 x>>8:1111 1111 1111 1111
Why doesnβt the same mechanism happen in the first case?
c ++ bit-manipulation bit-shift
router
source share