为什么我的C long不能存储比普通Int更大的数?

qvsjd97n  于 2023-10-16  发布在  其他
关注(0)|答案(2)|浏览(105)

由于某种原因,我不能得到4294967296或任何更大的数字存储在一个长。在第二次运行循环时,change变量在相乘后变为0,尽管它是一个长变量。

unsigned long interleave(unsigned int i1, unsigned int i2) {
    long int out = 0l;
    unsigned long r1;
    unsigned long r2;
    unsigned long change;
    change = 1l;
    unsigned long store;
    unsigned long bitSize = 256l;
    printf("%010x + %010x = ", i1, i2);
    //printf("START CHANGE %016x or %lu\n", change, change);
    for (int i = 3; i >= 0; i--) {
        //Get remainder for int 1;
        r1 = (unsigned long)i1 % (unsigned long)bitSize;
        i1 = (unsigned long)i1 / (unsigned long)bitSize;
        //Get remainder for int 2;
        r2 = (unsigned long)i2 % (unsigned long)bitSize;
        i2 = (unsigned long)i2 / (unsigned long)bitSize;
        //printf("GOT %04x and %04x\n", r1, r2);
        store = ((unsigned long)r1 * (unsigned long)bitSize) + (unsigned long)r2;
        store = (unsigned long)store * (unsigned long)change;
        //printf("STORE %016x\n", store);
        change = ((unsigned long)change) * ((unsigned long)bitSize);
        printf("CHANGE AFTER FIRST MULTIPLICATION: %016x or %lu\n", change, change);
        change = ((unsigned long)change) * ((unsigned long)bitSize);
        printf("CHANGE AFTER SECOND MULTIPLICATION: %016x or %lu\n", change, change);
        //printf("OUT: %016x\n", out);
        out += (unsigned long)store;
    }
    printf("%016x", out);

}

int main() {
    interleave((unsigned)4294967295, (unsigned)2999999);
    char* ch;
    scanf("%c%c", ch, ch);
}

我试过将数据类型切换为“long long”或“long int”或“long long int”。似乎没有一个工作。我现在已经明确地将所有athemic转换为“unsigned long”,但这也失败了。该函数应该在每个字节的基础上交织两个整数,因此“c245a1”和“1e67ba”应该交织到“c21e4567a1ba”。相反,由于某种原因,当我使用长它不能容纳任何大的,也不能一个“长长”。

jjjwad0x

jjjwad0x1#

unsigned long interleave(unsigned int i1, unsigned int i2) {
    unsigned long long int out = 0l;
    unsigned long long int r1;
    unsigned long long int r2;
    unsigned long long int change;
    change = 1ll;
    unsigned long long store;
    unsigned long long bitSize = 256l;
    printf("%08x + %08x = ", i1, i2);
    for (int i = 3; i >= 0; i--) {
        //Get remainder for int 1;
        r1 = (unsigned long long)i1 % (unsigned long long)bitSize;
        i1 = (unsigned long long)i1 / (unsigned long long)bitSize;
        //Get remainder for int 2;
        r2 = (unsigned long long)i2 % (unsigned long long)bitSize;
        i2 = (unsigned long long)i2 / (unsigned long long)bitSize;
        store = ((unsigned long long)r1 * (unsigned long long)bitSize) + (unsigned long long)r2;
        store = (unsigned long long)store * (unsigned long long)change;
        change = ((unsigned long long)change) * ((unsigned long long)bitSize);
        change = ((unsigned long long)change) * ((unsigned long long)bitSize);
        out += (unsigned long long)store;
    }
    printf("%016llx", out);
    return out;
}

感谢Barmar和dbush。原来我的系统上的长整型和整型一样大,我需要像Barmar建议的那样把它改成Long Long。然而我没有意识到这已经解决了问题,因为我需要在printf中使用“llx”而不是“x”,以便按照dbush的建议查看其余的数字。如果其他人也有这种问题,只需将您的长时间更改为长时间,看看是否可以修复它,并检查是否正确打印。

vhmi4jdf

vhmi4jdf2#

C标准仅指定类型所需的最小值,不保证long将大于int

(absolute value) to those shown, with the same sign.
— number of bits for smallest object that is not a bit-field (byte)
CHAR_BIT 8
— minimum value for an object of type signed char
SCHAR_MIN -127 // −(2 7 − 1)
— maximum value for an object of type signed char
SCHAR_MAX +127 // 2 7 − 1
— maximum value for an object of type unsigned char
UCHAR_MAX 255 // 2 8 − 1
— minimum value for an object of type char
CHAR_MIN see below
— maximum value for an object of type char
CHAR_MAX see below
— maximum number of bytes in a multibyte character, for any supported locale
MB_LEN_MAX 1
— minimum value for an object of type short int
SHRT_MIN -32767 // −(2 15 − 1)
— maximum value for an object of type short int
SHRT_MAX +32767 // 2 15 − 1
— maximum value for an object of type unsigned short int
USHRT_MAX 65535 // 2 16 − 1
— minimum value for an object of type int
INT_MIN -32767 // −(2 15 − 1)
— maximum value for an object of type int
INT_MAX +32767 // 2 15 − 1
— maximum value for an object of type unsigned int
UINT_MAX 65535 // 2 16 − 1
— minimum value for an object of type long int
LONG_MIN -2147483647 // −(2 31 − 1)
— maximum value for an object of type long int
LONG_MAX +2147483647 // 231 − 1
— maximum value for an object of type unsigned long int
ULONG_MAX 4294967295 // 2 32 − 1
— minimum value for an object of type long long int
LLONG_MIN -9223372036854775807 // −(2 63 − 1)
— maximum value for an object of type long long int
LLONG_MAX +9223372036854775807 // 2 63 − 1
— maximum value for an object of type unsigned long long int
ULLONG_MAX 18446744073709551615 // 2 64 − 1

相关问题