实际提炼的问题:为什么不显示0?#include "stdafx.h"#include <iostream>#include <string>int _tmain(int argc, _TCHAR* argv[]){ unsigned char barray[] = {1,2,3,4,5,6,7,8,9}; unsigned long weirdValue = barray[3] << 32; std::cout << weirdValue; // prints 4 std::string bla; std::getline(std::cin, bla); return 0;}移位操作的反汇编: 10: unsigned long weirdValue = barray[3] << 32;00411424 movzx eax,byte ptr [ebp-1Dh]00411428 shl eax,20h0041142B mov dword ptr [ebp-2Ch],eax原始问题:我在我们维护的一些旧代码中找到了以下代码片段。它将字节数组转换为多个浮点值,并将这些浮点添加到列表中。为什么它对大于4的字节数组有效?unsigned long ulValue = 0;for (USHORT usIndex = 0; usIndex < m_oData.usNumberOfBytes; usIndex++){ if (usIndex > 0 && (usIndex % 4) == 0) { float* pfValue = (float*)&ulValue; oValues.push_back(*pfValue); ulValue = 0; } ulValue += (m_oData.pabyDataBytes[usIndex] << (8*usIndex)); // Why does this work for usIndex > 3??}我会理解,如果ulValue += (m_oData.pabyDataBytes[usIndex] << (8*(usIndex%4)))但是像我发现的代码使我感到困惑。该代码是使用VS 2005编译的。如果我在即时窗口中尝试原始代码段,则无法正常运行。我知道如何正确执行此操作,我只想知道为什么代码特别是shift操作按原样工作。编辑:移位操作的反汇编为:13D61D0A shl ecx,3 // multiply uIndex by 813D61D0D shl eax,cl // shift to left, does nothing for multiples of 3213D61D0F add eax,dword ptr [ulValue]13D61D15 mov dword ptr [ulValue],eax所以拆卸很好。 最佳答案 移位计数被屏蔽为5位,这将范围限制为0-31。因此,偏移32与偏移零相同。http://x86.renejeschke.de/html/file_module_x86_id_285.html关于c++ - 为什么将值移动到比其大小大的位不会导致0?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41565850/
10-10 21:38