C++移位
我对移位位不熟悉,但我正在尝试调试以下代码片段:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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.确保按照 unsinged long long 工作之前的建议,对 tmp_data 使用 64 位 long 类型,但请检查您的编译器和体系结构文档。
代码应该如下所示。另外,请确保您的 demp_content_buff 被声明为 unsigned char * 或 unsigned char []。
然后将您的代码更改为:
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:
为了使移位运算符能够移位超过 32 位,
tmp_data
将需要是 64 位类型 - 正如其他几个答案所示。但是,您还希望
simulated_data
是某种 64 位类型的数组,否则tmp_data
将始终为零(或符号扩展的1< /code> 位)在分配其原始值时位于最高有效位中:
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 otherwisetmp_data
will always be zero (or a sign-extended set of1
bits) in the most significant bits when you assign its original value:如果您使用的是 Visual Studio,请确保将 tmp_data 定义为:
而不是
If you're using Visual Studio, make sure that you define tmp_data as:
instead of