Reputation: 13
I have a probably simple question, which I just cant seem to understand.
I am creating a serial parser for a datalogger which sends a serial stream. Under the documentation for the product a calculation is stated, which I don't understand.
Lateral = Data1 And 0x7F + Data2 / 0x100
If (Data1 And 0x80)=0 Then Lateral = -Lateral
What does Data1
And 0x7f
means? I know that 7F
is 127
, but besides that I don't understand the combination with the And statement.
What would the real formula look like?
Upvotes: 0
Views: 251
Reputation: 2176
1st sentence
Lateral = Data1 And(&) 0x7f + Data2/ 0x100
means take the magnitude of Data1
(Data and 0x7f) and add to it the value of Data2/256
2nd sentence
check the sign od Data1
and assign the same to Lateral
.
Upvotes: 0
Reputation: 5919
Bitwise AND -- a bit in the output is set if and only if the corresponding bit is set in both inputs.
Since your tags indicate that you're working in C, you can perform bitwise AND with the & operator.
(Note that 0x7F is 01111111 and 0x80 is 10000000 in binary, so ANDing with these correspond respectively to extracting the lower seven bits and extracting the upper bit of a byte.)
Upvotes: 1