c++ - C++ 仅更改 __int64 变量的低 32 位
问题描述
我有一个带有随机值的 int64 变量。我想将它的低 32 位修改为 0xf0ffffff
变量是 rdx 寄存器,但我想编辑 edx 值
ContextRecord->Rdx = 0xf0ffffff; // Not correct
解决方案
读取整个值,屏蔽低位,然后将其与您想要的 32 位值按位或:
#include <stdint.h>
void f(int64_t *X)
{
*X = (*X & ~(uint64_t)0xffffffff) //mask out the lower 32 bits
| 0xf0ffffff; //<value to set into the lower 32 bits
}
gcc
并且clang
在 little-endian 架构上将其优化为直接mov
进入低 32 位,即相当于:
#include <string.h>
//only works on little-endian architectures
void g(int64_t *X)
{
uint32_t y = 0xf0ffffff;
memcpy(X,&y,sizeof(y));
}
推荐阅读
- node.js - 无法将嵌套的 json 插入 cassandra
- python - python多处理 - 怎么了?
- node.js - 无法运行任何 Gulp 命令
- java - 在 SWT 中将 CSS 设置为树标题
- javascript - 编辑路径点或线的选择
- r - 在创建函数时将函数参数默认值设置为变量值
- sql-like - Ignite 中的 LIKE 运算符问题
- mysql - 如何使用pymysql将特殊字符插入mysql数据库
- php - JSON 删除不需要的字符
- c# - 在 Data Grid 的最后一个 Data Column 上放置一个上下文菜单来控制 WPF 中其他列的可见性