Search code examples
cbit-shift

Compute signed long max value in C using bit shift


Just started learning C yesterday and this is about to drive me crazy on the new year's day... Try to print the different int ranges using bit shift operations. Everything works fine apart from the signed long max/min value. Can't figure out why (1 << 63) - 1 returns -1? But (1 << 64) -1 for unsigned long long works fine...

#include <limits.h>
#include <stdio.h>

void print_range() {
    signed char scmax = (1 << 7) - 1;
    char c = scmax; // char means signed char!
    unsigned char uscmax = (1 << 8) - 1;
    char cmin = -(1 << 7);
    unsigned char ucmin = 0;
    printf("signed char max: %d = %d, unsigned char max: %d = %d\n", scmax, SCHAR_MAX, uscmax, UCHAR_MAX);
    printf("signed char min: %d = %d, unsigned char min: %d\n", cmin, CHAR_MIN, ucmin);

    // signed int
    int imax = (1 << 31) - 1; //(2 << 30) - 1;
    unsigned int uimax = (1 << 32) - 1;
    int imin = -(1 << 31);
    //NOTE: %d is for signed char/short/int, %u is for the unsigned formatter.
    printf("signed int max: %d = %d, unsigned int max: %u = %u\n", imax, INT_MAX, uimax, UINT_MAX);
    printf("signed int min: %d = %d, unsigned int min = %d\n", imin, INT_MIN, 0);

    long long lmax = (1 << 63) - 1L; // WHY DOES THIS NOT WORK???
    unsigned long long ulmax = (1 << 64) - 1;
    long long lmin = -(1 << 63); // NEITHER DOES THIS???
    printf("signed long max: %lld = %lld, unsigned long max: %llu = %llu\n", lmax, LLONG_MAX, ulmax, ULLONG_MAX);
    printf("signed long min: %lld = %lld, unsigned long min: %d\n", lmin, LLONG_MIN, 0);
}

Solution

  • The expression is evaluated as int as both operands are ints. You need to make them long longs:

    ((1LL << 63) - 1)
    (((long long)1 << 63) -1)
    

    Additionally, many architectures will shift by at most the size of the type -1, so it is shifted by just 31 bits for 63, and by 0 for 32 or 64.

    (1<<64)-1) works differently than expected: (1<<64) is 0 due to as explained in previous paragraph. 0-1 is -1 which is converted to long long, which is still -1LL and converted to unsigned long long it results into max unsigned long long (due to 2-complement representation of signed and unsigned numbers in common architectures)