BIt Comparison

Can anyone explain this line of code to me?

    H_dat = (((unsigned int)Hum_H) << 8) | Hum_L;

H_dat is defined as unsigned int
Hum_H is a byte
Hum_L is a byte

I'm not sure whats taking place on the left and side of the OR.

It moves the lower 8 bits into the most significant position then ORs the lower 8 bits into the least significant position.
Thus you end up with MSBLSB or a 16 bit integer

Aye wot he said.... i think?

the assignment is casting to a 2byte type (unsigned int) and shifting the 8 bits of HUM_H 8 positions to the left or into the 1st of the 2 bytes, leaving hex0000 or 00000000 in the low byte, and logical or of 000000000 with HUM_L will be HUM_L, si hi byte = HUM_H and low byte = HUM_L

i hate bitwise ops, they always make my head hurt.. and give me code myopia

Thanks for clarifying, I'm still a bit confused by this bit

((unsigned int)Hum_H)

The unsigned int will be 2 byte type and the Hum_H is a 1 byte correct? So what is occuring before the shift of the 8 bits?

Yettimania:
Thanks for clarifying, I'm still a bit confused by this bit

((unsigned int)Hum_H)

The unsigned int will be 2 byte type and the Hum_H is a 1 byte correct? So what is occuring before the shift of the 8 bits?

What is happening is the 8-bit value of Hum_H is expanded to a 16-bit value that can be shifted left by 8 bits without losing data. If you tried to shift an 8-bit value left 8 bits, you'll end up with 0 because the bits "fall off" the top of the 8-bit container.

H_dat is defined as unsigned int
We are casting Hum_H as a unsigned int. (unsigned int)

Hum_H lets say it is 11110000 one byte
Hum_L lets say it is 10101010 one byte

H_dat = ((unsigned int)Hum_H) << 8 then
Would give
H_dat = 11110000 << 8
H_dat = 1111000000000000

Then we do | Hum_L;

H_dat = 1111000000000000
Hum_L 10101010 OR

H_dat = 1111000010101010

Thanks everyone for the help. I guess I expected it to be structured different in the code.Something more like

unsigned int(HUM H) instead of ((unsigned int)HUM H) with the () opposite. Make it look more like a conversion.

I didn't realize you could compare 2 byte and 1 byte either with the OR statement. Thought you would have had to change Hum_L to 2 byte maybe shift then compare.

Everything is much more clear.

I didn't realize you could compare 2 byte and 1 byte either with the OR statement.

That code is not comparing.
It is creating a new 16 bit integer out of two 8 bytes.

It may be easier for you to "see" it using examples in the two bytes:

H_dat = (((unsigned int)Hum_H) << 8) | Hum_L;

H_dat is defined as unsigned int

unsigned int H_dat = B0000000000000000;  // initialized here as zero for example's sake

Hum_H is a byte

byte Hum_H = B11111111; // decimal 255 for example

Hum_L is a byte

byte Hum_L = B00000001; //decimal 1 for example

so first this happens:

((unsigned int) Hum_H) = B0000000011111111

The byte is cast into (converted to) an unsigned int

And then this happens:

(((unsigned int)Hum_H) << 8) = B1111111100000000

Which is an 8 bit shift to the 'left' or into the Most Significant 8 bits of the int.

then this happens:

(((unsigned int)Hum_H) << 8) | Hum_L = B1111111100000001

and is finally assigned to your unsigned int H_dat

H_dat = B1111111100000001

sorry bout the damn smileys but then the damn code boxes get too damn big, damn it!

Yettimania:
unsigned int(HUM H) instead of ((unsigned int)HUM H) with the () opposite. Make it look more like a conversion.

When you are experienced in C and C++, it already looks like a conversion.

(unsigned int)HUM_H -- This takes HUM_H, and does a type cast to an unsigned int. The basic syntax is: ( ) Where is any data type, and is something that is to be converted to that type. (Of course, not all type conversions make sense, for example, you can't directly cast an integer to a string, nor can you cast an array of function pointers into a structure, among many other possibilities...) But numeric types to other numeric types are generally allowed, as are integers to/from pointers, or one type of pointer into another pointer type.

unsigned int(HUM_H) This doesn't make syntactic sense. You are basically trying to call a function named int, passing it the value HUM_H. But then what is the unsigned doing in there? Or, perhaps you are declaring a function named int, which expects a parameter of type HUM_H, and returns an unsigned value (int is assumed when unsigned is used all by itself.) But you can't use a type name (like int) as the name of a function, and you probably haven't declared a type named HUM_H (and you can't have a type and variable with exactly the same name.) And you couldn't use this in the middle of an expression, because that is no place for a function declaration.

You'll get used to the syntax quickly enough, and in no time, (unsigned int)HUM_H will look perfectly natural to you.