[英]GCC : left shift operator on unsigned long long int does not work when shift is greater than 32?
考慮這個簡單的例子:
unsigned long long int my_int=0b10100110;
printf("%.64B\n",my_int<<40);
為什么 output 為零? 使用unsigned long long int
(sizeof = 8),64 位機器,操作系統:Fedora 37、gcc (GCC) 12.2.1 20221121 (Red Hat 12.2.1-4),以及使用或不使用 m64 標志的編譯。
對於初學者來說,這種格式的 integer 常量
0b10100110
不是標准的。
二進制 integer 常量在 C++ 有效。
此外 function printf
不支持轉換說明符B
。
在任何情況下,對於類型為unsigned long long int
的 output 和 object,您需要在轉換說明符之前使用長度修飾符ll
。
相反,您可以使用十六進制 integer 常量,例如
unsigned long long int my_int = 0xA6;
和 output 一樣
printf("%#.8llx\n",my_int<<40);
在這種情況下,output 看起來像
0xa60000000000
或者作為@chux - Reinstate Monica在評論中正確指出答案,您可以使用以下調用
printf("%#0.8llx\n",my_int<<40);
到 output 前綴0x
當相應的參數等於0
時。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.