Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use uint64_t in C [duplicate]

Tags:

c

#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?

like image 717
Ruby Sapphire Avatar asked Mar 28 '17 03:03

Ruby Sapphire


People also ask

What is uint64_t in C?

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.

Is uint64_t same as long long?

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.

Is there uint64_t?

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.

What is uint32_t in C?

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 Answers

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)

like image 80
user253751 Avatar answered Oct 19 '22 07:10

user253751