Stef1611
Stef1611

Reputation: 2387

GCC : left shift operator on unsigned long long int does not work when shift is greater than 32?

Considering this simple example :

unsigned long long int my_int=0b10100110;
printf("%.64B\n",my_int<<40);

Why is the output zero ? unsigned long long int is used (sizeof = 8), 64 bits machine, OS : Fedora 37, gcc (GCC) 12.2.1 20221121 (Red Hat 12.2.1-4), and compilation with or without the m64 flag.

Upvotes: 0

Views: 109

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311038

For starters this format of an integer constant

0b10100110

is not standard.

Binary integer constants are valid in C++.

Also the function printf does not support the conversion specifier B.

In any case to output an object of the type unsigned long long int you need to use length modifier ll before the conversion specifier.

Instead you could use a hexadecimal integer constant like for example

unsigned long long int my_int = 0xA6;

and output it like

printf("%#.8llx\n",my_int<<40);

In this case the output will look like

0xa60000000000

Or as @chux - Reinstate Monica correctly pointed in the comment to the answer you can use the following call

printf("%#0.8llx\n",my_int<<40);

to output the prefix 0x when the corresponding argument is equal to 0.

Upvotes: 3

Related Questions