简体   繁体   中英

What does hibyte = Value >> 8 meaning?

I am using C for developing my program and I found out from an example code

unHiByte = unVal >> 8;

What does this mean?? If unVal = 250 . What could be the value for unHiByte ??

I am really confused with this..Please Help..

Thanks in advance..

">>" in programming is a bitwise operation. The operation >> means shift right operation. So unVal >> 8 means shift right unVal by 8 bits. shifting the bits to the right can be interpreted as dividing the value by 2.

Hence, unHiByte = unval >> 8 means unHiByte = unVal/(2^8) (divide unVal by 2 eight times)

Without going into the shift operator itself (since that is answered already), here the assumption is that unVal is a two byte variable with a high byte (the upper 8 bits) and a low byte (the lower 8 bits). The intent is to obtain the value produced by ONLY the upper 8 bits and discarding the lower bits.

The shift operator though should easily be learned via any book / tutorial and perhaps was the reason some one down voted the question.

The >> is a bitwise right shift .

It operates on bits. With unHiByte = unVal >> 8; When unVal=250 .

Its binary form is 11111010

Right shift means to shift the bits to the right. So when you shift 1111 1010 , 8 digits to right you get 0000 0000 .

Note: You can easily determine the right shift operation result by dividing the number to the left of >> by 2^(number to right of >> )

So, 250/2 8 = 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM