C++移位

发布于 2024-08-25 09:22:28 字数 1647 浏览 2 评论 0原文

我对移位位不熟悉,但我正在尝试调试以下代码片段:

if (!(strcmp(arr[i].GetValType(), "f64")))
 {
        dem_content_buff[BytFldPos] = tmp_data;
     dem_content_buff[BytFldPos + 1] =  tmp_data >> 8;
     dem_content_buff[BytFldPos + 2] = tmp_data >> 16;
     dem_content_buff[BytFldPos + 3] = tmp_data >> 24;
     dem_content_buff[BytFldPos + 4] = tmp_data >> 32;
     dem_content_buff[BytFldPos + 5] = tmp_data >> 40;
     dem_content_buff[BytFldPos + 6] = tmp_data >> 48;
     dem_content_buff[BytFldPos + 7] = tmp_data >> 56;
        }    

我收到一条警告,称“32”到“56”的行的移位计数太大。谓词中的“f64”仅表示数据应该是64位数据。

这应该怎么做呢?

编辑:

我应该放入更多代码。

tmp_data = simulated_data[index_data];

if (!(strcmp(dems[i].GetValType(), "s32")))

{ dem_content_buff[BytFldPos] = tmp_data; dem_content_buff[BytFldPos + 1] = tmp_data>> 8; dem_content_buff[BytFldPos + 2] = tmp_data>> 16; dem_content_buff[BytFldPos + 3] = tmp_data>> 24;
if

(!(strcmp(dems[i].GetValType(), "f64"))) { dem_content_buff[BytFldPos] = tmp_data; dem_content_buff[BytFldPos + 1] = tmp_data>> 8; dem_content_buff[BytFldPos + 2] = tmp_data>> 16; dem_content_buff[BytFldPos + 3] = tmp_data>> 24; dem_content_buff[BytFldPos + 4] = tmp_data>> 32; dem_content_buff[BytFldPos + 5] = tmp_data>> 40; dem_content_buff[BytFldPos + 6] = tmp_data>> 48; dem_content_buff[BytFldPos + 7] = tmp_data>> 56; 因此

dem_content_buff 现在仅保存整数。我也不能使用这个数组来存储 64 位数据吗?

I am new to shifting bits, but I am trying to debug the following snippet:

if (!(strcmp(arr[i].GetValType(), "f64")))
 {
        dem_content_buff[BytFldPos] = tmp_data;
     dem_content_buff[BytFldPos + 1] =  tmp_data >> 8;
     dem_content_buff[BytFldPos + 2] = tmp_data >> 16;
     dem_content_buff[BytFldPos + 3] = tmp_data >> 24;
     dem_content_buff[BytFldPos + 4] = tmp_data >> 32;
     dem_content_buff[BytFldPos + 5] = tmp_data >> 40;
     dem_content_buff[BytFldPos + 6] = tmp_data >> 48;
     dem_content_buff[BytFldPos + 7] = tmp_data >> 56;
        }    

I am getting a warning saying the lines with "32" to "56" have a shift count that is too large. The "f64" in the predicate just means that the data should be 64bit data.

How should this be done?

edit:

I should have put more of the code in.

tmp_data = simulated_data[index_data];

if (!(strcmp(dems[i].GetValType(), "s32")))

{
dem_content_buff[BytFldPos] = tmp_data;
dem_content_buff[BytFldPos + 1] = tmp_data >> 8;
dem_content_buff[BytFldPos + 2] = tmp_data >> 16;
dem_content_buff[BytFldPos + 3] = tmp_data >> 24;
}

if (!(strcmp(dems[i].GetValType(), "f64")))
{
dem_content_buff[BytFldPos] = tmp_data;
dem_content_buff[BytFldPos + 1] = tmp_data >> 8;
dem_content_buff[BytFldPos + 2] = tmp_data >> 16;
dem_content_buff[BytFldPos + 3] = tmp_data >> 24;
dem_content_buff[BytFldPos + 4] = tmp_data >> 32;
dem_content_buff[BytFldPos + 5] = tmp_data >> 40;
dem_content_buff[BytFldPos + 6] = tmp_data >> 48;
dem_content_buff[BytFldPos + 7] = tmp_data >> 56;
}

So, dem_content_buff right now only holds ints. Can I not use this array for the 64 bit data either?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吝吻 2024-09-01 09:22:29

tmp_data 的类型是什么?

我的猜测是 tmp_data 只有 32 位,因此出现错误。它需要是 64 位无符号 int,这可以使用非标准(但得到良好支持)unsigned long long 数据类型来实现。

What is the type of tmp_data?

My guess is that tmp_data is only 32-bits, hence the error. It will need to be a 64-bit unsigned int, which is can be achieved using the non-standard (but well-supported) unsigned long long data type.

各空 2024-09-01 09:22:29

确保按照 unsinged long long 工作之前的建议,对 tmp_data 使用 64 位 long 类型,但请检查您的编译器和体系结构文档。

代码应该如下所示。另外,请确保您的 demp_content_buff 被声明为 unsigned char * 或 unsigned char []。

然后将您的代码更改为:

if (!(strcmp(arr[i].GetValType(), "f64")))
    {
        dem_content_buff[BytFldPos] =  ( tmp_data & 0xff );
        dem_content_buff[BytFldPos + 1] =  (tmp_data >> 8 ) & 0xff ;
        dem_content_buff[BytFldPos + 2] =  (tmp_data >> 16) & 0xff ;
        dem_content_buff[BytFldPos + 3] =  (tmp_data >> 24) & 0xff;
        dem_content_buff[BytFldPos + 4] =  (tmp_data >> 32) & 0xff;
        dem_content_buff[BytFldPos + 5] =  (tmp_data >> 40) & 0xff;
        dem_content_buff[BytFldPos + 6] =  (tmp_data >> 48) & 0xff;
        dem_content_buff[BytFldPos + 7] =  (tmp_data >> 56) & 0xff;
        }   

Make sure to use 64bit long type for the tmp_data as was suggested before the unsinged long long should work, but check your compiler and architecture documentation.

The code should look like this. Also, make sure that your demp_content_buff is declared as unsigned char * or unsigned char [].

Then change your code to:

if (!(strcmp(arr[i].GetValType(), "f64")))
    {
        dem_content_buff[BytFldPos] =  ( tmp_data & 0xff );
        dem_content_buff[BytFldPos + 1] =  (tmp_data >> 8 ) & 0xff ;
        dem_content_buff[BytFldPos + 2] =  (tmp_data >> 16) & 0xff ;
        dem_content_buff[BytFldPos + 3] =  (tmp_data >> 24) & 0xff;
        dem_content_buff[BytFldPos + 4] =  (tmp_data >> 32) & 0xff;
        dem_content_buff[BytFldPos + 5] =  (tmp_data >> 40) & 0xff;
        dem_content_buff[BytFldPos + 6] =  (tmp_data >> 48) & 0xff;
        dem_content_buff[BytFldPos + 7] =  (tmp_data >> 56) & 0xff;
        }   
故事和酒 2024-09-01 09:22:29

为了使移位运算符能够移位超过 32 位,tmp_data 将需要是 64 位类型 - 正如其他几个答案所示。

但是,您还希望 simulated_data 是某种 64 位类型的数组,否则 tmp_data 将始终为零(或符号扩展的 1< /code> 位)在分配其原始值时位于最高有效位中:

tmp_data = simulated_data[index_data];

For the shift operator to work shifting more than 32 bits, tmp_data will need to be a 64-bit type - as several other answers indicated.

However, you would also want simulated_data to be an array of some 64-bit type otherwise tmp_data will always be zero (or a sign-extended set of 1 bits) in the most significant bits when you assign its original value:

tmp_data = simulated_data[index_data];
独闯女儿国 2024-09-01 09:22:29

如果您使用的是 Visual Studio,请确保将 tmp_data 定义为:

__int64 tmp_data;              // Visual studio version

而不是

unsigned long long tmp_data;   // GCC version

If you're using Visual Studio, make sure that you define tmp_data as:

__int64 tmp_data;              // Visual studio version

instead of

unsigned long long tmp_data;   // GCC version
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文