Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 字段打包以形成单个字节_Objective C_C_Hex_Bitwise Operators - Fatal编程技术网

Objective c 字段打包以形成单个字节

Objective c 字段打包以形成单个字节,objective-c,c,hex,bitwise-operators,Objective C,C,Hex,Bitwise Operators,我正在努力学习如何将四个独立的值打包成一个字节。我试图得到一个十六进制输出0x91,二进制表示应该是10010001,但是我得到的输出分别是:0x1010001和16842753。还是有更好的方法 uint8_t globalColorTableFlag = 1; uint8_t colorResolution = 001; uint8_t sortFlag = 0; uint8_t sizeOfGlobalColorTable = 001; uint32_t packed = ((gl

我正在努力学习如何将四个独立的值打包成一个字节。我试图得到一个十六进制输出
0x91
,二进制表示应该是
10010001
,但是我得到的输出分别是:
0x1010001
16842753
。还是有更好的方法

uint8_t globalColorTableFlag = 1;

uint8_t colorResolution = 001;

uint8_t sortFlag = 0;

uint8_t sizeOfGlobalColorTable = 001;

uint32_t packed = ((globalColorTableFlag << 24) | (colorResolution << 16) | (sortFlag << 8) | (sizeOfGlobalColorTable << 0));

NSLog(@"%d",packed); // Logs 16842753, should be: 10010001
NSLog(@"0x%02X",packed); // Logs 0x1010001, should be: 0x91
uint8\u t globalColorTableFlag=1;
uint8_t颜色分辨率=001;
uint8\u t sortFlag=0;
uint8_t sizeOfGlobalColorTable=001;
uint32\u t packed=((globalColorTableFlag请尝试以下操作:

/* packed starts at 0 */
uint8_t packed = 0;

/* one bit of the flag is kept and shifted to the last position */
packed |= ((globalColorTableFlag & 0x1) << 7);
/* three bits of the resolution are kept and shifted to the fifth position */
packed |= ((colorResolution & 0x7) << 4);
/* one bit of the flag is kept and shifted to the fourth position */
packed |= ((sortFlag & 0x1) << 3);
/* three bits are kept and left in the first position */
packed |= ((sizeOfGlobalColorTable & 0x7) << 0);
/*压缩从0开始*/
uint8\u t packed=0;
/*标志的一位被保留并移动到最后一个位置*/

packed |=((globalColorTableFlag&0x1)
packed=((globalColorTableFlag&1)尝试NSLog(@“%x”,packed)你会看到正在发生的事情。问题不在于算术,而在于你的NSLog格式。@CharlieBurns我试过了,现在我得到:
1010001
缺少一个
0
位不,不是。你希望在哪里看到另一个零?哦,我明白了。前导零被剥离。你在生成一个32位整数,为什么期望一个8位t值?:)谢谢你花时间回答这个问题!你能解释一下它的作用吗?
packed = ((globalColorTableFlag & 1) << 7) +
    ((colorResolution & 0x7) << 4) +
    ((sortFlag & 1) << 3) +
    ((sizeOfGlobalColorTable & 0x7);