#include <stdio.h>
#include <stdint.h>
int main(){
uint64_t a = 1 << 63;
/* do some thing */
return 0;
}
$ gcc -Wall -Wextra -std=c99 test.c -o test
warning: left shift count >= width of type [-Wshift-count-overflow]
Q: uint64_t
should have 64 bits width, why the left shift operation overflows?
The UInt64 value type represents unsigned integers with values ranging from 0 to 184,467,440,737,095,551,615. UInt64 provides methods to compare instances of this type, convert the value of an instance to its string representation, and convert the string representation of a number to an instance of this type.
So unsigned long long is the same as uint64_t in the 32-bit compilation but not in 64-bit compilation? Yes. In 32-bit mode, most likely long is 32 bits and long long is 64 bits. In 64-bit mode, both are probably 64 bits.
uint64_t doesn't call anything, it is just a 64bit unsigned integer. This code is a function named clearBits() that is manipulating bits in an integer and returning the result.
uint32_t is a numeric type that guarantees 32 bits. The value is unsigned, meaning that the range of values goes from 0 to 232 - 1. This. uint32_t* ptr; declares a pointer of type uint32_t* , but the pointer is uninitialized, that is, the pointer does not point to anywhere in particular.
1
is an int
which is either only 32 bits on your platform, or it could be 64 bits but signed.
Use (uint64_t)1 << 63
to cast 1
to 64-bit unsigned integer first. (Or ((uint64_t)1) << 63
if you prefer)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With