【发布时间】:2015-04-24 17:26:35
【问题描述】:
根据我的最后一个问题:Field packing to form a single byte
但是,根据这些值,我得到了意想不到的结果。顶部代码示例给了我0x91 的预期输出,但是如果我将colorResolution 和sizeOfGlobalColorTable 变量更改为: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);
【问题讨论】:
标签: objective-c c bit-manipulation bitwise-operators