Math Help.. Explanation if Possible...

sodeaf

Senior Member
Hey there,

Just trying to figure out what the following actuality means, for I am not sure on the symbols being used, and or the Terminology. The Sample below is from a friend using a different program/uC, I was wondering if the picaxe is capable of completing the task as well.
I am trying to utilize some Thermal ticket Dispensers, but in order for them to actually print the last 2 Bytes of information must be correct. They are based on the Calculation of all bytes send to printer.

I would just like to be able to understand this Logic? Type of math?

VAR_T = (VAR_R ^ VAR_I) & $0F
VAR_R = (VAR_R >> 4) ^ (VAR_T * $1081)

VAR_H = VAR_R & $FF
VAR_L = VAR_R >> 8

Thanks

Steve
 

westaust55

Moderator
The abbreviated symbols can differ between languages but the seem similar to the PICAXE BASIC commands other than PICAXE does not have brackets.

If you look in PICAXE manual 2 page 137: http://www.picaxe.com/docs/picaxe_manual2.pdf
the symbolic alternatives to the more "english-like" versions of the comamnd label/name text is given

Thus:
^ is exclusive OR
& is AND
>> is shift right for X1 and X2 parts

Some of those VAR_<x> variables might be word sized and capable of holding values greater than 255.
You might need some further information from your friend about the value ranges for the variables. Also is the word variables low then high byte format ? (Big Endian versus Little Endian)
See here: https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Data/endian.html

In the past and likely still Intel and Zilog microprocessors used the Big Endian format whereas Motorola and MosTechnology microprocessors used the Little Endian format - as does PICAXE


If you look at the last two statements, in PICAXE construct
VAR_H = VAR_R & $FF
VAR_R is a word variable.
VAR_H is a byte variable (H suggests High byte but by PICAXE math suggests low byte and AND'ing with $FF (or $00FF) masks of the other byte
VAR_L is also a byte variable (L suggests low byte but again PICAXE variant gives high byte) and then >> 8 shifts right an entire byte (8 bits) to extract the second byte.​
 
Last edited:

hippy

Ex-Staff (retired)
VAR_T = (VAR_R ^ VAR_I) & $0F
VAR_R = (VAR_R >> 4) ^ (VAR_T * $1081)
That would seem to be calculating some sort of 16-bit checksum in VAR_R.

VAR_H = VAR_R & $FF
VAR_L = VAR_R >> 8
That would appear to be putting the low byte of the checksum into an 8-bit byte variable VAR_H and the high byte of the checksum into a 8-bit byte variable VAR_L.
 

sodeaf

Senior Member
Hey Guys,

Cant thank you enough Westaust55 and Hippy.. Your detailed explanations make complete sense. I was able to rework the code and get it working. After removing brackets, I had to add a couple lines of code and it worked out great.
Quick question based on the the couple lines that actually retrieve the answer.

Symbol VAR_R = W6

VAR_H = VAR_R & $FF
VAR_L = VAR_R >> 8

Do I really need those calculations, for the answer I am looking is already separated in B12 and B13 for the 16 bit is W6. Seems to work as well.. Just curious in regards if those 2 lines are actually doing something different I'm not seeing when using DEBUG

W6 = 44498 = $ADD2
B12 = $D2
B13 = $AD

VAR_H = $D2
VAR_L = $AD

Thanks again..

Steve
 

westaust55

Moderator
Hey Guys,

Cant thank you enough Westaust55 and Hippy.. Your detailed explanations make complete sense. I was able to rework the code and get it working. After removing brackets, I had to add a couple lines of code and it worked out great.
Quick question based on the the couple lines that actually retrieve the answer.

Symbol VAR_R = W6

VAR_H = VAR_R & $FF
VAR_L = VAR_R >> 8

Do I really need those calculations, for the answer I am looking is already separated in B12 and B13 for the 16 bit is W6. Seems to work as well.. Just curious in regards if those 2 lines are actually doing something different I'm not seeing when using DEBUG

W6 = 44498 = $ADD2
B12 = $D2
B13 = $AD

VAR_H = $D2
VAR_L = $AD

Thanks again..

Steve
As you have determined, since there is an overlap between the pre-defined word variables and the byte variables in the PICAXE parts, such that W6 = b13:b12
there is no need to calculate the high and low bytes VAR_H and VAR-L as you already have them in B13 and B12 respectively.
 
Top