使用位移计算C中有符号的长最大值 [英] Compute signed long max value in C using bit shift

查看:86
本文介绍了使用位移计算C中有符号的长最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天刚开始学习C,这将在新的一年让我发疯……尝试使用位移运算来打印不同的int范围.除了有符号的最大/最小长整型值之外,一切都正常.无法弄清楚为什么(1 << 63) - 1返回-1?但是unsigned long long(1 << 64) -1可以正常工作...

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);
}

推荐答案

由于两个操作数均为int,因此表达式的计算结果为int.您需要将它们设置为long long s:

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)

此外,许多架构最多将移位类型-1的大小,因此对于63,它仅移位31位,对于32或64,仅移位0.

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)的工作原理与预期的不同:(1<<64)0,原因如前所述. 0-1-1,将其转换为long long,仍然是-1LL,并转换为unsigned long long,结果为最大unsigned long long(由于signedunsigned的2补码表示)在常见的架构中)

(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)

这篇关于使用位移计算C中有符号的长最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆