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...

Recommended Answers

All 2 Replies

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.

edit:

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.

commented: Perfect explaination +1

Cool thanks. Makes perfect sense now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.