Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4484

Java • Java Help with bitwise operators

$
0
0
I'm using borrowed code, that I sort of understand, but then I get a little lost.
I need some help

Code:

       byte[] data = new byte[2];       data[0] = (byte)device.readRegister(0x32);        data[1] = (byte)device.readRegister(0x33);              int xAccl = ((data[1] & 0x03) * 256 + (data[0] & 0xFF));        if(xAccl > 511){          xAccl -= 1024;
The context is an ADXL345 accelerometer on Raspberry Pi 4B - doesn't really matter. It runs, I get data out.
I'm running the accelerometer in high resolution mode, so its output is 13 bit binary.. The data comes in two bytes that need to be combined.
I see the more significant byte is multiplied by 256 - makes sense - before combining with the less signicant byte.

Questions -
- The bitwise & 0x03 seems to be reducing combined bit length to 10 bits.
--- I believe that may be an error if the original data was 13 bit - is that correct?
-- How is this trying to 'filter' the value? If my data was 13 bit, is there value in & 00011111 ?

- The bitwise & 0xFF seems meaningless..
--- Wouldn't a bitwise AND with 11111111 be the same as the original byte value?
-- What is gained by doing this AND?

What is the point of the if > 511? is this checking for negative values?
If I know my original data 13 bit should I be checking if > 4095 ?

Thanks
JMD

Statistics: Posted by JMDFlorida — Wed Jul 31, 2024 1:49 am — Replies 5 — Views 73



Viewing all articles
Browse latest Browse all 4484

Trending Articles