Math L->R, what is "the answer?"

JoeFromOzarks

Senior Member
Howdy folks!

I haven’t been sleeping much lately, medical issues, but when I’m bothered by a question, I stay on it until my eyes bleed. (Ignore that statement if you’re a bit queasy.) answer = 1*8 + 0*4 + 0*2 + 1*1

PE5 and PE6 both return 65, simulated and with a 14M2.

Code:
answer = 1*8 + 0*4 + 0*2 + 1*1

Happy Saturday!! Thank you!! joe

Last edited:

AllyCat

Senior Member
Hi Joe,

PICaxe maths works purely in sequence from left to right, so:

1*8 = 8
8 + 0 = 8
8 * 4 = 32
32 + 0 = 32
32 * 2 = 64
64 + 1 = 65
65 * 1 = 65

There is no normal "operator precedence" (* and / calculated before + and -) which would give:

1*8 + 0*4 + 0*2 + 1*1 = 8 + 0 + 0 + 1 = 9

And it can't be fixed by using brackets (). So you need something like:

b1 = 1 * 8 ; = 8
b1 = 0 * 4 + b1 ; = 8
b1 = 0 * 2 + b1 ; = 8
b1 = 1 * 1 + b1 ; = 9

Cheers, Alan.

Last edited:

pxgator

Senior Member
Hi Joe,

PICAXE does math STRICTLY from left to right!! So, with that in mind
65 is computed because of the STRICT left to right rule.

Cheers to All

JoeFromOzarks

Senior Member
Thank you guys!

Okay. I see the error of my ways. Who knows what I was thinking. 1*8+0*4+0*2+1*1 yep, that is clearer.

I was working on: answer=bit3*8 + bit2*4 ' + bit1*2 + bit0, OOPS!

Today, I've not had any breakfast and only one Pepsi. joe

Hi Joe,

PICaxe maths works purely in sequence from left to right, so:

1*8 = 8
8 + 0 = 8
8 * 4 = 32
32 + 0 = 32
32 * 2 = 64
64 + 1 = 65
65 * 1 = 65

There is no normal "operator precedence" (* and / calculated before + and -) which would give:

1*8 + 0*4 + 0*2 + 1*1 = 8 + 0 + 0 + 1 = 9

And it can't be fixed by using brackets (). Cheers, Alan.

hippy

Technical Support
Staff member
answer = bit3 * 2 + bit2 * 2 + bit1 * 2 + bit0

Will give you what you want. If you really are literally using 'bit3' through 'bit0' variables you can do that simpler and quicker with -

JoeFromOzarks

Senior Member
What has me in a discombobulated state is a play with RANDOM w0. Over 1000 operations, the first 999 “RANDOM w0” returns the same (I mean EXACTLY the same) ->order<- of results. Using a 14M2, the first 26 results: (The leftmost value is w0, the rightmost value is the process.)
Code:
1 0
4 1
1 2
4 3
14 4
11 5
15 6
9 7
13 8
15 9
13 10
11 11
12 12
9 13
15 14
13 15
12 16
5 17
9 18
5 19
6 20
4 21
1 22
6 23
1 24
10 25
Remember me mentioning a Pepsi? Off I go… Maybe there is a nap in my future. Maybe.

joe

JoeFromOzarks

Senior Member
Thank you Hippy, I love your solution. joe

answer = bit3 * 2 + bit2 * 2 + bit1 * 2 + bit0

Will give you wnaht you want correctly. If you really are using 'bit3' through 'bit0' you can do that simpler with -

AllyCat

Senior Member
Hi,

We need to see your actual code, but I suspect that you are "corrupting" the value in w0, perhaps when you "print" the data.

FWIW, this "code snippet" thread goes into considerable detail. Cheers, Alan.

JoeFromOzarks

Senior Member

I'm sure I'm doing something very goofy here, in the below code, I mean.

I have a 14M2 on a breadboard. Python is running on a computer reading the SERTXD line, no problem. When the LED turns on, Python verifies the "----End" text is at the end, closes the file, saves it with a unique file name, F####.TXT where #### is the number of the file, 0001 to 1000. Easy. When the file is closed, Python resets the PICAXE (turns off PICAXE Vcc through a P-MOSFET) for five seconds then turns it on, the cycle is repeated 1000 times.

Meanwhile, here is the code:

Code:
#PICAXE 14M2
#TERMINAL 4800
#NO_DATA

PAUSE 1000
SYMBOL oLED1  = C.0
SYMBOL nPass  = w13
nPass = 0
SERTXD ("----Start",cr,lf)  ' tell Python to start logging
LOW oLED1

MAIN:

DO
RANDOM w11
b2 = w11 / 199 // 16 + 1 ' pick a number between 1 and 15
LOOP UNTIL b2 <> b3
b3 = b2
SERTXD (#b2," ",#nPass,CR,LF)
nPass = nPass + 1
IF nPass > 999 THEN
GOTO ENDIT
ENDIF

GOTO MAIN

ENDIT:
HIGH oLED1
SERTXD ("----End",cr,lf)
; wait for Python to reboot PICAXE
DO
LOOP
; finished

Each and every F####.TXT has EXACTLY the same results. (I know the data is unique for each save as the file header has filename, date, time.)

Man, when I'm tired I can go on tangents even a goofy squirrel would find amusing.  joe

Oh. No "seed."

:/ joe

JoeFromOzarks

Senior Member
The Bear,

I think I'm thick as a brick:    joe

hippy

Technical Support
Staff member
Oh. No "seed."

:/ joe
Think you solved it One quick and easy way to re-seed, at least so it doesn't repeat the same every power-up -

Code:
PowerOnReset:
b0 = b0 + 1
Write 0, b0

MainLoop:
Do
Random w0
:
Loop

JoeFromOzarks

Senior Member
Thank you Hippy, I'll give it a try!!

(Right after I locate the "smack my forehead" icon!)  joe

Think you solved it One quick and easy way to re-seed, at least so it doesn't repeat the same every power-up -

Code:
PowerOnReset:
b0 = b0 + 1
Write 0, b0

MainLoop:
Do
Random w0
:
Loop

inglewoodpete

Senior Member
If you want a "more random" sequence and you have a spare input pin with ADC, you can add a voltage divider comprising a resistor and an LDR that is exposed to ambient light. Read the ADC10 value as your seed number at bootup. Any change to light levels will change the seed value.