Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
Joined: 18/11/2011 Location: United KingdomPosts: 3805
Posted: 03:48pm 22 May 2023
Copy link to clipboard
Print this post
There is not really a standard, just as there isn't for the order of (let's call them bytes) with a (let's say word) :(
On a good day the data sheet makes it clear. The rest of the time you guess. When guessing I start with LE (little endian) for bytes (when relevant) and lowest order bit in data sheet to be bit 0 in a byte/word.
Sometimes a data sheet gives a program snippet or a web search finds working examples so you can avoid guessing.
Short of finding a software example, I suspect phil99 will be right in this case.
John
lew247
Guru
Joined: 23/12/2015 Location: United KingdomPosts: 1702
Posted: 03:55pm 22 May 2023
Copy link to clipboard
Print this post
Phil Where did you get the 8 and 255 from?
lew247
Guru
Joined: 23/12/2015 Location: United KingdomPosts: 1702
Posted: 04:03pm 22 May 2023
Copy link to clipboard
Print this post
0x60 ; Prescaler second byte 0x34 ; Prescaler first byte
IanRogers
Senior Member
Joined: 09/12/2022 Location: United KingdomPosts: 151
Posted: 04:52pm 22 May 2023
Copy link to clipboard
Print this post
He is shifting the 16 bit number into two bytes..
6148 = b' 0001 1000 0000 0100
>> 8 will lose the bottom 8 digits AND 255 will lose the top 8 digits splitting them into High byte and Low byteI'd give my left arm to be ambidextrous
JohnS Guru
Joined: 18/11/2011 Location: United KingdomPosts: 3805
Posted: 04:59pm 22 May 2023
Copy link to clipboard
Print this post
AND 255 may be easier to grasp if you think of it as AND &hFF
A byte of all 1s is FF; the AND keeps those bits which are 1.
John
phil99
Guru
Joined: 11/02/2018 Location: AustraliaPosts: 2140
Posted: 01:26am 23 May 2023
Copy link to clipboard
Print this post
Where did you get the 8 and 255 from?
Apologies for not clarifying. As explained above >>8 moves the top 8 bits down to make the high byte and the bottom 8 bits drop off the edge (... of the World. Past the Elephants it's Turtles all the way down). 255 is my lazy way of writing &B11111111.
An alternative method to split the bytes.
HighByte = 6148 \ 256 '256 = 2^8, integer division discards the remainder (anything below 1) LowByte = 6148 Mod 256 'Modulo division discards the result and restores the remainder - the low 8 bits in this case.
These however, use more clock cycles than Shift and AND.
Edit. Tried timing it 100,000 times and the difference is insignificant.
> timer=0:for m=0 to 100000:p=6148\256:n=6148 mod 256:next:? timer 6542.66 > timer=0:for m=0 to 100000:p=6148>>8:n=6148 and 255:next:? timer 6276.764 >