计算符号整数的最大尺寸 [英] Calculating the maximum size of a signed integer

查看:159
本文介绍了计算符号整数的最大尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道的最大值我的 time_t的可容纳是,所以我写了一个小程序,帮助我。它需要一个参数:字节数(1字节= 8位)。所以我写了它,并对其进行了测试。它顺利通过所有值从1直到4,但在5日和更高它也编辑签署位(我不知道它是如何称呼)。有人可以解释:

I wanted to know what the maximum value my time_tcan hold was, so I wrote a little program helping me. It needs one argument: the amount of bytes (1 byte = 8 bits). So I wrote it and tested it. It goes well by all values from 1 untill 4, but at 5 and higher it also edits the "signed"-bit (I don't know how it's called). Can someone explain:

#include <stdio.h>

int main(int argc, const char **argv) {
    if(argc != 2) {
    fprintf(stderr, "Usage: %s bits/8\n", argv[0]);
    return -1;
    }

    unsigned int bytes;
    sscanf(argv[1], "%u", &bytes);

    unsigned int i;
    signed long long someInt = 0;
    size_t max = bytes*8-1;
    for(i = 0; i < max; i++) {
    someInt |= 1 << i;
    }

    /* Print all bits, we substracted 
    1 to use in the previous loop so 
    now we add one again */
    max++;
    for(i = 0; i < max; i++) {
    int isAct = (someInt >> max-i-1) & 1;
    printf("%d", isAct);
    if((i+1) % 8 == 0) {
        printf(" ");
    }
    }
    printf("\n");

    printf("Maximum size of a number with %u bytes of 8 btis: %lld\n", bytes, (long long)someInt);

    return 0;
}

我的测试:

Script started on Sun Jan 30 16:34:38 2011
bash-3.2$ ./a.out 1
01111111 
Maximum size of a number with 1 bytes of 8 btis: 127
bash-3.2$ ./a.out 2
01111111 11111111 
Maximum size of a number with 2 bytes of 8 btis: 32767
bash-3.2$ ./a.out 4
01111111 11111111 11111111 11111111 
Maximum size of a number with 4 bytes of 8 btis: 2147483647
bash-3.2$ ./a.out 5
11111111 11111111 11111111 11111111 11111111 
Maximum size of a number with 5 bytes of 8 btis: -1
bash-3.2$ ./a.out 8
11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 
Maximum size of a number with 8 bytes of 8 btis: -1
bash-3.2$ exit
exit

Script done on Sun Jan 30 16:35:06 2011

我希望从这样一来学习,所以我真的AP preciate,如果有人能找到一些时间来看看这个。

I'm hoping to learn from this one, so I'd really appreciate it if someone can find some time to take a look at this.

ief2

推荐答案

您只使用了 INT ,即 1 ,为你的换档操作。这

You are only using an int, namely 1, for your shift operation. This

someInt |= 1LL << i;

会做的更好,我觉得。

would do better, I think.

通常是没有办法,我知道的有符号整数类型的最大值,其中你只有一个typedef,而不用担心的未定义行为的或编译器和平台的特定属性。例如在&LT;&LT; 运营商可能会对签订各类问题

Generally there is no way that I know of to have the maximum value of a signed integer type of which you only have a typedef without risking undefined behavior or compiler and platform specific properties. E.g the << operator may have problems on signed types.

time_t的特别奇怪,因为它可能是一个浮点型或整型。如果是,如果它被签署或不它没有被指定的整数。

time_t is particularly odd since it might be a floating point type or an integer type. And if it is an integer it is not specified if it is signed or not.

如果你认为这是一个有符号整数类型的的,你没有所谓的填充位(大多数平台上符合了)最大值,可直接计算

If you suppose that it is a signed integer type and that you don't have so-called padding bits (most platforms conform to that) the maximum value can be computed directly

((((1LL << (sizeof(time_t)*CHAR_BIT-2)) - 1) << 1) + 1)

无需溢出。

这篇关于计算符号整数的最大尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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