简体   繁体   中英

c bit manipulation (endianess)

Could someone explain me this code please ? I have received some byte code from an assembler and now I have to use it in my virtual machine. This code is used but I don't know how it works and what it is used for.

static int32_t bytecode_to_int32 (const uint8_t* bytes)
{
  uint32_t result = (uint32_t)bytes[0] << 24 |
                    (uint32_t)bytes[1] << 16 |
                    (uint32_t)bytes[2] <<  8 |
                    (uint32_t)bytes[3] <<  0 ;
  return (int32_t)result;
}

It builds up a 32 bit word from 4 bytes. For example if the bytes are : 1st: 0x12 , 2nd: 0x34, 3rd: 0x56, 4th: 0x78 Then:

static int32_t bytecode_to_int32 (const uint8_t* bytes)
{
  uint32_t result = (uint32_t)bytes[0] << 24 | // -> 0x12000000
                    (uint32_t)bytes[1] << 16 | // -> 0x00340000
                    (uint32_t)bytes[2] <<  8 | // -> 0x00005600
                    (uint32_t)bytes[3] <<  0 ; // -> 0x00000078
  return (int32_t)result; // bitwise oring this result -> 0x12345678
}

This function attempts to combine the four bytes in a uint8_t[4] into a single uint32_t with big-endian byte order, cast the result into a signed int32_t , and return that.

So, if you pass a pointer to the array { 0xAA, 0xBB, 0xCC, 0xDD } to the function, it will combine them into a 32-bit integer with the most significant bytes of the integer coming from the lowest addresses in the array, giving you 0xAABBCCDD or -1430532899 .

However, if the array pointed to by the argument bytes is not at least four bytes long, it has undefined behavior.

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