gpt4 book ai didi

objective-c - 字段打包字节返回意外结果

转载 作者:太空宇宙 更新时间:2023-11-04 01:26:43 26 4
gpt4 key购买 nike

根据我的最后一个问题:Field packing to form a single byte,我一直在尝试更改字段打包字节的某些位的值。

但是,根据这些值,我得到了意想不到的结果。顶部代码示例为我提供了 0x91 的预期输出,但是如果我将 colorResolutionsizeOfGlobalColorTable 变量更改为:010,我得到了 0x80 的意外输出,这不是它应该是什么的二进制表示:10100010 基于此处:http://www.best-microcontroller-projects.com/hex-code-table.html .我希望底部代码示例的输出为:0xA2。我缺少或不理解什么?

此代码正确记录:0x91

uint8_t screenDescriptorPackedFieldByte = 0;

uint8_t globalColorTableFlag = 1;
uint8_t colorResolution = 001;
uint8_t screenDescriptorSortFlag = 0;
uint8_t sizeOfGlobalColorTable = 001;

screenDescriptorPackedFieldByte |= ((globalColorTableFlag & 0x1) << 7);
screenDescriptorPackedFieldByte |= ((colorResolution & 0x7) << 4);
screenDescriptorPackedFieldByte |= ((screenDescriptorSortFlag & 0x1) << 3);
screenDescriptorPackedFieldByte |= ((sizeOfGlobalColorTable & 0x7) << 0);

NSLog(@"0x%02X",screenDescriptorPackedFieldByte);

此代码错误记录:0x80

uint8_t screenDescriptorPackedFieldByte = 0;

uint8_t globalColorTableFlag = 1;
uint8_t colorResolution = 010;
uint8_t screenDescriptorSortFlag = 0;
uint8_t sizeOfGlobalColorTable = 010;

screenDescriptorPackedFieldByte |= ((globalColorTableFlag & 0x1) << 7);
screenDescriptorPackedFieldByte |= ((colorResolution & 0x7) << 4);
screenDescriptorPackedFieldByte |= ((screenDescriptorSortFlag & 0x1) << 3);
screenDescriptorPackedFieldByte |= ((sizeOfGlobalColorTable & 0x7) << 0);

NSLog(@"0x%02X",screenDescriptorPackedFieldByte);

最佳答案

这个值不是二进制的。是octal .

uint8_t sizeOfGlobalColorTable = 010;

(Objective) C 中,从 0 开始的常量被解释为八进制值。你实际写的是b1000 & b0111 = 0

应该是:

uint8_t sizeOfGlobalColorTable = 0x2;

关于objective-c - 字段打包字节返回意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29853807/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com