English 中文(简体)
Bug在将一个沥青阵列改装为uint64时
原标题:Bug when converting a byte array to a uint64
  • 时间:2023-10-12 23:56:43
  •  标签:
  • c

I m试图在将星阵列转换成uint64_t时标出一个灯泡(unsign long)。

我知道你可以这样做,但这不是我的问题。 我希望以多种重复的方式这样做。 我想知道,为什么在有些情形下,重复做法没有按计划进行。

该法典的结果是正确的:

#include <stdio.h>

int main()
{
    unsigned char byte[8];
    unsigned long long x;

    byte[0] = 0x15;
    byte[1] = 0x15; 
    byte[2] = 0x17;
    byte[3] = 0x18; 
    byte[4] = 0x19;
    byte[5] = 0x20;
    byte[6] = 0x21;
    byte[7] = 0x12;
            
    x = (byte[0]*0x100000000000000) +
        (byte[1]*0x1000000000000) + 
        (byte[2]*0x10000000000) + 
        (byte[3]*0x100000000);

    x = x +
        (byte[4]<<24) + 
        (byte[5]<<16) + 
        (byte[6]<<8) + 
        byte[7];

    printf("%llx
", x);
}

结果是15171819202112,这是正确的。

但就本案而言,我用这些 by子代替:

byte[0] = 0x15;
byte[1] = 0x92;  // changed 
byte[2] = 0x53;  // changed
byte[3] = 0x22;  // changed
byte[4] = 0xec;  // changed
byte[5] = 0x33;  // changed
byte[6] = 0x99;  // changed
byte[7] = 0x12;

......结果是错误的。

I get:
15925321ec339912.

But, it should be:
15925322ec339912.

Why? What is the bug?

问题回答

在这一次低压中:

byte[4]<<24

页: 1 这一数值首先见promotedint。 详情见。 关于算术转换的标准:

The following may be used in an expression wherever an int or unsigned int may be used:

  • An object or expression with an integer type (other than int or unsigned int) whose integer conversion rank is less than or equal to the rank of int and unsigned int.
  • A bit-field of type _Bool, int, signed int, or unsigned int.

If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions. All other types are unchanged by the integer promotions.

因此,<代码>逐[4]的升值有<代码>int的型号,已签署,(很可能)32比值。 然后,将<条码>int值留给24位。 鉴于原始价值为0xec,因此,价值1的数值被移至所产生的<代码>int数值的标尺。

上,将1个字移入了标栏。 http://www. open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf” rel=“nofollow noreferer” 关于双向转移操作者的标准:

The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1 × 2E2, reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and nonnegative value, and E1 × 2E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.

您可以首先将数值投到<代码>,长期寄出<> 代码>,以便转换有效。

x=x+((unsigned long long)byte[4]<<24)+(byte[5]<<16)+(byte[6]<<8)+byte[7];




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签