Binary Maths

tobyw_1969

New Member
I have been scratching my head to try and find ways to reduce code size..

I had some code to do this sort of thing,

Code:
if pin1 = 1 then
   selection = 1 
   gosub on_selection
else if pin2 = 1 then
   selection = 2
   gosub on_selection
else if pin3 = 1 then
   selection = 3
   gosub on_selection
end if
but I thought it might be possible to just convert the state of the input pins into a single number 1-3, like

Code:
selection = pin1 + (pin2*2) + (pin3*3)
if selection > 0 then gosub on_selection
it would save some bytes, but the problem is, I can't do this, because brackets are only available on x2 parts and I am trying to fit this into an 18M

so i have been racking my brains for binary maths ideas, and searching through the commands available (setbit, toggle etc) to try and find an answer, but I can't work it out

for those of you who weren't made to stand in the corner every maths lesson for organising compass fights - is there a simple way to create a number from 1-3 based on the individual 0/1 values of 3 variables? I thought it might be possible to use shift, but that's not supported on 18M :(
 
Last edited:

Technical

Technical Support
Staff member
Could you just modify on_selection to work with 2,4,8 rather than 1,2,3
Then it is (assuming only one pin ever on)

selection = pins & %00001110
gosub on_selection
 

tobyw_1969

New Member
It would be handy if I could, but I really need it to be 1,2 and 3. I don't think there is a way, based on the available maths commands - it seems like you can't shift binary numbers either on 18M :(

Also, I think you have found the major flaw in the concept - if more than one button is on, it would break. I hate the idea of having to have three byte-hungry if statements, but I don't see any way around it for polling the buttons...
 

westaust55

Moderator
It would be handy if I could, but I really need it to be 1,2 and 3. I don't think there is a way, based on the available maths commands - it seems like you can't shift binary numbers either on 18M :(
To shift a binary number left one bit:
variable = variable * 2

To shift a binary number right one bit
variable = variable / 2

with X1 and X2 parts the NCD command could have been useful in conjunction with technical's answer but you now have identified another issue to resolve


EDIT:
Brackets are still to come even for the X1 and X2 parts . . . .


It might also help if you gave more information on your code.

What are the inputs. As you say you now realise you might have multiple switches at once (still solvable)

What in your code dictates that the variable "selection" must be 1,2,3,4, 5 and not 1,2,4,8 etc ?

The more info you provide the better the help you may receive.
 
Last edited:

tobyw_1969

New Member
westaust

Code:
the more info you provide the better the help you may receive
Why do you say that? Technical's answer was first rate, just like every other response on this forum. Looks like what I wanted to do is not possible, suggested alternative, and a warning about a flaw in the concept.
 

hippy

Ex-Staff (retired)
You can use this ...

LookDown 1, ( 0, pin1, pin2, pin3, 1 ), selection
If selection < 4 Then Gosub on_selection

That reduces the code size by about half.
 

tobyw_1969

New Member
That's genius! And it also won't break if two buttons are down at the same time - just give priority to the first one checked. You should open a paypal account at your nearest pub so people can buy you drinks online.

I also realised as I was driving up the motorway this morning that Technical has given me another answer to the problem, because as he points out, you can have multiple key presses. So I can use that in my favour - connect the input sources to the buttons accumulatively with diodes in between so that the first input sends pin1 high, second input sends pins 1 & 2 high and third input sends all three high. Then I could just count up the states of the pins and get 1-3. I can't test because I am not at home yet, but it sounds plausible. I saw that wiring being suggested on here I think, as a solution to setint limitations, but it sounds like it would work for this too.

I guess if you wanted other numbers as the output, you would still be left with if statements, unless like others suggest, you could design your code to work with the numbers derivable by looking at pins.

Thanks all as usual.:)
 

westaust55

Moderator
westaust

Code:
the more info you provide the better the help you may receive
Why do you say that? Technical's answer was first rate, just like every other response on this forum. Looks like what I wanted to do is not possible, suggested alternative, and a warning about a flaw in the concept.
Not suggesting the responses you received are poor by any stretch of the imagination.

My thought were more to lack of info in your statement that might help others. When I first read your post .

my first thought was: you code only looks for one input and could miss others. Or are they exclusive? (only ever one at a time)

Sure by your second post you indicated you had realised your problem.

You indicated "I had some code to do this sort of thing "
so how many inputs in the final code -? unclear as it was a sort of example.

hence my suggestion that some more information might be helpful.

then you suggest there was no shift command to which I gave you a solution using divide and multiply.

Sorry you feel my comments are not appropriate or helpful but I do not have a crystal ball to divine the full picture.
 
Last edited:

tobyw_1969

New Member
WestAust - what is all this about crystal balls? I clearly said TWICE in the first post that I was using an 18M...if you can't be bothered to read and understand the original question, then keep your sarcy comments to yourself please, and let the grown ups like Technical and Hippy respond. Which they did, with the same information you had, only very helpfully.
 

kevrus

New Member
I would like to come to Westaust's defence here and say that I very much doubt that his comments were meant as sarcasm.
It is often the case that a solution to a problem is easier to acomplish when given as much information as possible, otherwise people make educated guesses based on their own interpretation of the issue...(often bieng not totally accurate due to a misunderstanding of the problem), and Westaust one of the first on this forum to offer help and advice to others.
 
Top