简体   繁体   中英

Binary representation of a char array

I have a union, that is 8 fields of uint8, 2 fields of int32, or one int64.

I am trying to save an array of 8 values there: 0, 0, 0, 1, 0, 0, 0, 1

There is a bug in the software that I am trying to solve unfortunately, I don't understand the binary representation of this array.

My union:

union {
    uint8_t u08[8];
    uint32_t u32[2];
    uint64_t u64;
} data;

My debugger output:

Name : u08
Details:"\0\0\0\001\0\0\0\001"
Default:0x4001e6a0 <ucHeap+66948>
Decimal:1073866400
Hex:0x4001e6a0
Binary:1000000000000011110011010100000
Octal:010000363240

Name : u32
Details:{1, 1}
Default:0x4001e6a0 <ucHeap+66948>
Decimal:1073866400
Hex:0x4001e6a0
Binary:1000000000000011110011010100000
Octal:010000363240

Name : u64
Details:4294967297
Default:4294967297
Decimal:4294967297
Hex:0x100000001
Binary:100000000000000000000000000000001
Octal:040000000001

My questions are:

  1. why does a u64 have a different value than the others? as far as I understand, c saves space only for one variable of a union to store the value.
  2. what does this binary representation of the uint8 array mean? Even if it was ASCII (which it isn't, as I am sending direct values, not chars), then '0' is 110000 and '1' is 110001

I'm using MPC5748G from NXP with S32DS (eclipse based IDE) for the debugging.

What you see is not you think you see. Everything is correct there. u08 & u32 are arrays. u64 is an integer value and the debugger displays different information

Name : u08
Details:"\0\0\0\001\0\0\0\001"    -- actual values stored in the array
Default:0x4001e6a0 <ucHeap+66948> -- address of the array u08
Decimal:1073866400                -- same address in different bases
Hex:0x4001e6a0
Binary:1000000000000011110011010100000
Octal:010000363240

Name : u32
Details:{1, 1}                     -- actual values stored in the array
Default:0x4001e6a0 <ucHeap+66948>  -- address of the array u32
Decimal:1073866400                 -- same address in different bases
Hex:0x4001e6a0
Binary:1000000000000011110011010100000
Octal:010000363240

Name : u64
Details:4294967297                 -- value stored un the u64 integer
Default:4294967297                 -- same value in different bases
Decimal:4294967297
Hex:0x100000001
Binary:100000000000000000000000000000001
Octal:040000000001

{0, 0, 0, 1, 0, 0, 0, 1} is in big endian representation:

  • 0x00000001 ( 1 ) in u32
  • 0x0000000100000001 ( 0x100000001 without zeroes) in u64

if you want to see the same information for all the members you need to make the last member an array:

union {
    uint8_t u08[8];
    uint32_t u32[2];
    uint64_t u64[1];
} data;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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