How to Use Uint64_T in C [Duplicate]
#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?

3

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)

2
Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest