简体   繁体   English

将uint64_t截断为uint8_t [i]时,我应该使用位掩码吗?

[英]Should I use a bit mask when truncating uint64_t to uint8_t[i]?

If I have a large int, say a uint64_t, and an array of uint8_t, eg: 如果我有一个大的int,比如一个uint64_t和一个uint8_t数组,例如:

uint64_t large = 12345678901234567890;
uint8_t small[5];

and I want to copy the 8 least significant bits of the uint64_t into an element of the array of uint8_t , is it safe to just use: 我想将uint64_t的8个最低有效位复制到uint8_t数组的元素中,使用它是否安全:

small[3] = large;

or should I use a bit-mask: 或者我应该使用位掩码:

small[3] = large & 255;

ie Is there any situation where the rest of the large int may somehow overflow into the other elements of the array? 即是否有任何情况,其中大型int的其余部分可能以某种方式溢出到数组的其他元素?

It will most certainly not cause data to be processed incorrectly. 它肯定不会导致数据处理不正确。 However, some compilers may generate a warning message. 但是,某些编译器可能会生成警告消息。

There are two options to avoid these. 有两种方法可以避免这些。

You can cast your variable: 你可以投射你的变量:

(uint8_t)large

Or you can disable the warning: 或者您可以禁用警告:

#pragma warning(disable:4503)

I would suggest casting the variable, because hiding compiler warnings will potentially keep you from spotting actual problems and is therefore not best practice. 我建议转换变量,因为隐藏编译器警告可能会使您无法发现实际问题,因此不是最佳实践。

This is perfectly safe : 非常安全

small[3] = large;

and such a conversion is explicitly described in [conv.integral]: 并且[conv.integral]中明确描述了这种转换:

If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2 n where n is the number of bits used to represent the unsigned type). 如果目标类型是无符号的,则结果值是与源整数一致的最小无符号整数(模2 n ,其中n是用于表示无符号类型的位数)。

That is, these four statements all are guaranteed to end up with the same value in small[3] : 也就是说,这四个语句都保证在small[3]以相同的值结束:

small[3] = large;
small[3] = large % 256;
small[3] = large & 255;
small[3] = static_cast<uint8_t>(large);

there's no functional reason to do the % or & or cast yourself, though if you want to anyway I would be surprised if the compiler didn't generate the same code for all four (gcc and clang do). 有没有功能上的原因做%&或投自己,但如果你想反正我会感到惊讶,如果编译器未生成所有四个(gcc和铛做)相同的代码。

The one difference would be if you compile with something like -Wconversion , which would cause this to issue a warning (which can sometimes be beneficial). 一个区别是,如果您使用-Wconversion东西进行编译,这会导致发出警告(这有时可能是有益的)。 In that case, you'll want to do the cast. 在这种情况下,你会想要演员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该使用C类型(uint8_t /…/ uint64_t)还是(u_int8_t /…/ u_int64_t)? - Should I use C types (uint8_t/…/uint64_t) or (u_int8_t/…/u_int64_t)? 为什么我必须在*左移之前将&#39;uint8_t`转换为&#39;uint64_t` *? - Why must I cast a `uint8_t` to `uint64_t` *before* left-shifting it? 将 uint64_t 转换为 uint8_t[8] - Convert uint64_t to uint8_t[8] 我该如何使用<uint64_t>在一个数组中 - How do I use <uint64_t> in an array 为什么在 64 位机器上使用 uint8_t 编写的这段代码比使用 uint32_t 或 uint64_t 编写的类似代码运行得更快? - Why does this piece of code written using uint8_t run faster than analogous code written with uint32_t or uint64_t on a 64bit machine? 高阶位 - 取出它们并将uint64_t转换为uint8_t - High Order Bits - Take them and make a uint64_t into a uint8_t uint64_t 数组和 uint8_t 数组之间的 cpp 标准样式转换 - cpp std-style conversion between uint64_t array and uint8_t array 转换 std::vector<uint8_t> 打包 std::vector<uint64_t></uint64_t></uint8_t> - Convert std::vector<uint8_t> to packed std::vector<uint64_t> C ++为什么uint64_t和uint8_t上的按位运算符〜会返回不同的类型? - C++ Why bitwise operator ~ on uint64_t and uint8_t return different types? 与 uint64_t 斗争 - Struggling with uint64_t
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM