6

I am new to this, so I will begin by saying that while I was looking over some code I realized that this function doesn't make one bit of sense to me.

As you can see that this specific function uses bitwise operators to convert 4 unsigned char elements into integer.

//Converts a four-character array to an integer, using little-endian form

int toInt(const char* bytes) {
    return (int)(((unsigned char)bytes[3] << 24) |
                 ((unsigned char)bytes[2] << 16) |
                 ((unsigned char)bytes[1] << 8) |
                 (unsigned char)bytes[0]);
}

short toShort(const char* bytes) {
    return (short)(((unsigned char)bytes[1] << 8) |
                   (unsigned char)bytes[0]);
}

I already know how bitwise operators and how char uses 1 byte and int uses 4 bytes. Why would moving char bits 24 bits to the left and than just explicitly converting it to int convert it into an int? Why is bitwise operators necessary for this function?

This function goes beyond my comprehension, please explain this code and how it works or at least give me a link that throughly explains this.

I have looked everywhere for the explanation but could not find it.

This probably has a simple enough explanation.

1
  • Using operator << on a type shorter than int automatically promotes the input to an int. The final int cast is unnecessary. Commented Nov 22, 2014 at 2:25

1 Answer 1

6

Why is bitwise operators necessary for this function?

Bitwise operators are used to "assemble" the four-byte number from four single-byte numbers.

Let's say you've got four 8-bit numbers, like this:

aaaaaaaa
bbbbbbbb
cccccccc
dddddddd

Shifts give you this:

aaaaaaaa000000000000000000000000
00000000bbbbbbbb0000000000000000
0000000000000000cccccccc00000000
000000000000000000000000dddddddd

Bitwise operator OR lets you make a single number from these four parts, because OR-ing any bit x with a zero produces x. If you align four-byte numbers like shown above, there is only one non-zero bit in each position, so bitwise ORs produce the desired result:

aaaaaaaabbbbbbbbccccccccdddddddd
Sign up to request clarification or add additional context in comments.

5 Comments

I tried to sum up this answer in a comment, but I couldn't. Nice answer.
in this case how do unsigned character shift 24, 16, 8 bits to get integers? This doesn't make any sense.
@Snake: fair question. Read "Conversions" under en.cppreference.com/w/cpp/language/operator_arithmetic: "before any other action (but after lvalue-to-rvalue conversion, if applicable), the operand undergoes integral promotion."
@Snake it's because of integral operation promotion rules from C. << will always promote both operands to at least int before doing its work.
@Snake That's a good point: shifting is an integer operation, so it casts its operands to int before the shift, so an eight-bit aaaaaaaa becomes 000000000000000000000000aaaaaaaa before shifting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.