Ref:unsigned int 2 ulong type promotion issue in GCC!

Liu Hao lh_mouse@126.com
Sun May 24 13:50:52 GMT 2020


在 2020/5/24 21:31, Thamilarasu Kandasamy (thamil) via Gcc-help 写道:
> 
>     int size = 100;
>     unsigned int usize = 100;
> 
>     result1 = (void *) (((uintptr_t) &var) & ~(size - 1));
>     result2 = (void *) (((uintptr_t) &var) & ~(usize - 1));
>     result3 = (void *) (((uintptr_t) &var) & ~((unsigned long  int)usize - 1));
> 

`result1` is bitwise AND `&var` with `(uintptr_t)~(int)99` which is
`(uintptr_t)(int)-100` which is probably `0xFFFFFFFFFFFFFF9C` on a
64-bit machine.

`result2` is bitwise AND `&var` with `(uintptr_t)~(unsigned)99` which is
likely `(uintptr_t)(unsigned)4294967196` which is `0xFFFFFF9C` on a
64-bit machine. So higher half is truncated as expected.

`result3` is bitwise AND `&var` with `(uintptr_t)~(unsigned long)99`
which is probably `0xFFFFFFFFFFFFFF9C` on a 64-bit machine.


There is no bug.


-- 
Best regards,
LH_Mouse

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <https://gcc.gnu.org/pipermail/gcc-help/attachments/20200524/1dd0e7be/attachment.sig>


More information about the Gcc-help mailing list