简体   繁体   English

如何将64位数转换为8位

[英]how to convert 64 bit number to a 8 bit

How would I convert a 64 bit number ( 0xffffffffffffff8c ) to an 8 bit number ( 0x8c ). 如何将64位数字( 0xffffffffffffff8c )转换为8位数字( 0x8c )。 My code: 我的代码:

uint64_t val = 0xffffffffffffff8c;
uint8_t var = (uint8_t) (val & 0x0000FF);

Am I doing it the right way? 我是以正确的方式做到的吗?

uint64_t val = foo();

Consider 4 below methods, all well behaved because assigning a value to an unsigned type is well defined, even if the value is outside the range of the destination. 考虑下面的4个方法,所有方法都表现良好,因为很好地定义了为无符号类型赋值,即使该值超出了目标的范围。 Effectively it is the Euclidean mod of "one plus the max value of the destination type". 实际上,它是“一加上目标类型的最大值”的欧几里得模型

Note: The leading 0 s in mask constant 0x0000FF are not needed. 注意:不需要掩码常量0x0000FF中的前导0 Use leading zeros as desired to convey clarity. 根据需要使用前导零来表达清晰度。 Could use 0xFF or 0x00000000000000FF . 可以使用0xFF0x00000000000000FF


OP's original: No problem, but a bit verbose. OP原创:没问题,但有点冗长。 As long as val is some integer type, & is allowed. 只要val是某种整数类型,就允许使用&

uint8_t var = (uint8_t) (val & 0x0000FF);

Cast, no mask. 演员,没有面具。 Casting to (uint8_t) is a mask of 0xFF . 转换为(uint8_t)0xFF的掩码。 In general , masking should be avoided as it has surprising effects in porting code. 通常 ,应该避免屏蔽,因为它在移植代码时具有令人惊讶的效果。 Not so when casting to an unsigned fixed width type as in this simply example. 当转换为无符号固定宽度类型时,不是这样,只是在这个简单的例子中。

uint8_t var_nm = (uint8_t) val;

Mask, no cast. 面具,没有演员。 Like the above. 像上面这样。 Narrowing of the value is explicitly coded. 缩小值是明确编码的。

uint8_t var_nc = val & 0xFF;

No cast, no mask. 没有演员,没有面具。 This works buts may generate a warning such as below when the pedantic [-Wconversion] or equivalent is used. 当使用迂腐[-Wconversion]或等效时,这可能会产生警告,如下面的警告。 It is not clear that code expects a narrowing. 目前尚不清楚代码是否希望缩小范围。

// warning: conversion to 'uint8_t {aka unsigned char}' from 'uint64_t 
// {aka long long unsigned int}' may alter its value [-Wconversion]
uint8_t var_nc_nm = val;

The goal of coding is to 1) do the function 2) do it reasonable efficient 3) promote clarity of intent both now and in the future. 编码的目标是1)执行功能2)合理有效3)促进现在和将来的意图清晰度。 Recommend as it clearly conveys code's intention to narrow the value of the range 0-FF 建议,因为它清楚地传达了代码的意图,缩小范围0-FF的值

uint8_t var_nc = val & 0xFF;

As a second choice, go for simply 作为第二选择,简单地说

uint8_t var = val;

Expect the same executable code to be generated. 期望生成相同的可执行代码。

-- -

Minor: the names val and var are too generic. 次要: 名称 valvar太通用了。

You don't need the & nor the cast (although you may leave it to make the intent clearer). 你不需要&也不需要演员(虽然你可以留下它以使意图更清晰)。 You can freely convert implicitly between different integer types, and for unsigned integers the overflow behavior (=wraparound, which actually boils down to "taking only the low byte" here) is well defined (and does what you want here). 你可以在不同的整数类型之间隐式转换,对于无符号整数,溢出行为(=环绕,实际上归结为“只取低字节”这里)是很好定义的(并且在这里做你想要的)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM