- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我注意到它的输出完全错误时,我正在测试一个简单的编译器。事实上,输出的字节顺序从小变大了。经过仔细检查,违规代码原来是这样的:
const char *bp = reinterpret_cast<const char*>(&command._instruction);
for (int i = 0; i < 4; ++i)
out << bp[i];
四字节指令被重新解释为一组单字节字符并打印到标准输出(这很笨拙,是的,但那个决定不是我的)。为什么要交换这些位对我来说似乎不合逻辑,因为 char 指针首先应该指向最重要的(在这个 x86 系统上)位。例如,给定 0x00...04,char 指针应指向 0x00,而不是 0x04。属于后者。
我创建了一个简单的代码演示:
代码
#include <bitset>
#include <iostream>
#include <stdint.h>
int main()
{
int32_t foo = 4;
int8_t* cursor = reinterpret_cast<int8_t*>(&foo);
std::cout << "Using a moving 8-bit pointer:" << std::endl;
for (int i = 0; i < 4; ++i)
std::cout << std::bitset<8>(cursor[i]) << " "; // <-- why?
std::cout << std::endl << "Using original 4-byte int:" << std::endl;
std::cout << std::bitset<32>(foo) << std::endl;
return 0;
}
输出:
Using a moving 8-bit pointer:
00000100 00000000 00000000 00000000
Using original 4-byte int:
00000000000000000000000000000100
最佳答案
It doesn't seem logical to me why the bits would be swapped, since the char pointer should be pointing to the most-significant (on this x86 system) bits at first.
在 x86 系统上,指向多字节对象基址的指针并不指向最高有效字节,而是指向最低有效字节。这称为“小端”字节顺序。
在 C 中,如果我们获取一个占用多个字节的对象的地址,并将其转换为 char *
,它指向对象的基址:被认为位于最低有效地址,指针可以从该地址进行正位移(使用 +
或 ++
等)以到达其他字节。
关于c++ - reinterpret_cast 交换位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28145361/
我不知道在互联网上搜索此类 friend 的正确关键字,仅靠关键字 friend 不会带来预期的结果。 class Integer { friend int; }; friend int 是什么
我有两个数组,我想知道它们是否具有共同元素的最简单方法。所以实际上这个问题必须要提问。 string[] countries1 = new string[] { "USA", "Uruguay", "
我需要一个很好的资源来阅读如何创建一个算法来获取两条折线(一条由多条线组成的路径)并在它们之间执行并集、减法或交集。这与自定义 API 相关,因此我需要了解底层算法。 此外,任何 VB 方言的源代码都
我有一个生成简单 JPEG 图像的 C# 应用程序。我需要能够存储这些图像并在未来的不同时间调用它们。因此,我正在寻找一个交 key 的、安全的 CDN 系统。我已经将自己的服务器与 Windows
我是 ARKit 的新手,在检查了一些示例代码后,如 https://developer.apple.com/sample-code/wwdc/2017/PlacingObjects.zip我想知道是
例如,我想知道绿色是否会是在 div 标记内的字符 AAA 和 BBB 上,用于所有文档类型,例如 xhtml、html5、html? AAABBB 但我试过了,AAA 将是绿色,BBB 将是黑色作为
来 self 的问题 Insert element to ArrayList with ascending order and no duplicate elements 我已经完成了插入方法。 现在
我正在使用 Adobe LiveCycle ES 2 交 key 项目,它在 JBoss AS 4.2.1.GA 上运行。我在让 JNDI 绑定(bind)在 JBoss 中工作时遇到问题。这似乎
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提出有关书籍、工具、软件库等建议的问题。您可以编辑问题,以便可以用事实和引用来回答它。 去年关闭
我正在帮助一家典型的小公司,该公司从几个外包系统(谷歌应用程序、svn/trac)开始。添加了一个内部 jabber 服务器(ejabber 主要用于 iChat 客户端)。订阅几个网络服务(例如 h
假设我有以下代码(一个简单的 CRTP 类层次结构)。我想对基类类型进行 typedef 以节省自己的输入(在我的实际代码中,我多次使用基类类型并且基类采用多个模板参数),并且我需要与基类交 frie
我是一名优秀的程序员,十分优秀!