我一直在嘗試用包裝一個字節的基礎上,我的最後一個問題變化值,一些位現場: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);
我不希望使用十六進制值作爲輸入,我想使用0或1作爲我的標誌。 – klcjr89
@ aviatorken89就我在Google看到的,Objective-C中沒有二進制文字,所以你必須處理它。在使用Hex的時候會給你工作代碼。 – Alex
或者我可以刪除前導零,一切都會好?這對我來說很好,如果這樣做會產生有效的字節。 – klcjr89