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.
To convert a signed integer to an unsigned integer, or to convert an unsigned integer to a signed integer you need only use a cast. For example: int a = 6; unsigned int b; int c; b = (unsigned int)a; c = (int)b; Actually in many cases you can dispense with the cast.
unsigned short is 2 bytes long and bit mask will be 0010000000000000 ( 0x2000 ) however with unsigned char this value will be 10000000 ( 0x80 ).
The syntax is like below. unsigned char ch = 'n'; Both of the Signed and Unsigned char, they are of 8-bits. So for signed char it can store value from -128 to +127, and the unsigned char will store 0 to 255.
You can convert an int to an unsigned int . The conversion is valid and well-defined. Since the value is negative, UINT_MAX + 1 is added to it so that the value is a valid unsigned quantity. (Technically, 2N is added to it, where N is the number of bits used to represent the unsigned type.)
If you use a compile-time option to change the default for the char type to unsigned, the conversions given in the Conversions from unsigned integral types table for the unsigned char type apply, instead of the conversions in this table. In the Microsoft compiler, int and long are distinct but equivalent types.
As you can see that this specific function uses bitwise operators to convert 4 unsigned char elements into integer.
An unsigned integer is converted to a shorter unsigned or signed integer by truncating the high-order bits, or to a longer unsigned or signed integer by zero-extending.
In the Microsoft compiler, unsigned (or unsigned int) and unsigned long are distinct but equivalent types. Conversion of an unsigned int value proceeds in the same way as conversion of an unsigned long. The following table summarizes conversions from unsigned integral types.
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 OR
s produce the desired result:
aaaaaaaabbbbbbbbccccccccdddddddd
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With