the binary number 10001000 isn't it decimal number 136? It doesn't really matter but just to make sure i read binary numbers right.
Ah Yes, you're right it's 136, my bad
So the buf array simply just cuts it of right after 8 bit and continues at the next one. So if we read them each at the time and add them together we will get the wrong number. So when adding two bytes together how do we now which one is the first and second part?
Good question. So you come to the part of understanding how your accelerometer actually sends data. This can vary from component to component, so you will always have to read the datasheet to understand exactly how it's sent. The accelerometer in the example will send them. Take a look at page 32 in the datasheet over here
. The image shows how the 10bit data is stored in two 1-byte registers on the ADXL345. The code sample requests 6 bytes starting from address 0x32 to be stored in buf. So, it just iterates along the registers and copies them into buf in order.
Highly recommend reading the datasheet for any component you use. There's a lot to learn from there.
the (int) says that it will create and integer (2 bytes = 16 bits)? the buf gives the first 8 bits ans the << 8 tells to add 8 bits with value 0? Am I right at any point?
Almost right there. Just that <<8 shifts 8 bits to the left. By left, I mean towards the higher significant bits. When you shift them to the left, old bits on the left are pushed out into space and discarded and new bits on the right default to 0.
If you'd like to start getting your hands dirty on some advanced stuff on the arduino, you'll need to be good at bit math. I would strongly suggest reading the bitmath article I posted earlier. It covers all the basics that you encounter on a regular basis. I don't want to go further into bitshifting or how the OR works because it's explained very well in the article.
Btw, so far, we haven't talked about I2C. For that, I would second the link given by robtillaart, very good and detailed explanation by Nick Gammon with code samples. Also, this video by Jeremy Blum[/url] is a pretty good intro to I2C