Hello, I have the following pseudo code that I am trying to implement in Java. My issue is the pseudo code seems to be heavily C based and my C is very rusty. Could someone help me understand the following function?

uint theFunction(BYTE* header)
    UINT length;

    header = header + 1;
    length = ((UINT16) header[0]) | ((UINT16) header[1] << 8);

    return length;

My issues are mostly with the understanding of the header = header + 1; and the length = ((UINT16) header[0]) | ((UINT16) header[1] << 8);. I am assuming the header pointer is increasing to the 2nd byte and then the 3rd and 4th bytes are being or'd together after shifting the 4th...

4 Years
Discussion Span
Last Post by kmachstang

It wants an array of bytes and extracts a "length" value from that array by combining the bits in the second and third byte to form a 16 bit integer. (later stored in a 32 bit one..)

The most significant part of this 16-bit integer is formed by the third byte, the least significant section by the second byte.


example. Say you have this array as input, showing each byte as 8 bits.

[0000 0000][0101 0101][1111 0000]

The function would then produce a 16 bit integer like this:

[1111 0000 0101 0101]

This is stored into a 32-bit integer and then returned.

Edited by Gonbe

Votes + Comments
Perfect explaination
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.