bit of maths help please

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times

bit of maths help please

Post by Steve001 »

Morning all , hope everyone had a good xmas :mrgreen:

I am stuck with this one, i have a project which reads all of the inputs for example "PORT C" and returned a value which can be 0 - 255 i want to be able to convert this into 1,2,3,4,5,6,7,8 so if i have a value of 20 i want to get an answer of 5 and 3 .
It has been a long time since i have done 2's Compliment stuff and i have forgot now :(

Steve
Success always occurs in private and failure in full view.

User avatar
Steve
Matrix Staff
Posts: 3434
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times

Re: bit of maths help please

Post by Steve »

There are more efficient ways, but you could do this in a "long" (but perhaps more intuitive) way. For example, something similar to the following...

Code: Select all

pin = 8
mask = 128

loop 8 times
  if (portc_val >= mask)
    //val found
    output pin
   
    portc_val = portc_val - mask
  endif

  pin = pin - 1
  mask = mask / 2
end loop
I'm not sure how you would want to output the result (especially as the result can be anything from zero to 8 different numbers), so I'll leave that part to you.

Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times

Re: bit of maths help please

Post by Steve001 »

Thanks for that steve, will have a go with that later
Success always occurs in private and failure in full view.

Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times

Re: bit of maths help please

Post by Steve001 »

trying to understand on how the example works

pin = 8
mask = 128

loop 8 times
if (portc_val >= mask)
//val found
output pin

portc_val = portc_val - mask
endif

pin = pin - 1
mask = mask / 2
end loop

Dont understand this . .

mask = 128

Should this be 255 ? if not why is it 128 ? or am i missing something :?
Success always occurs in private and failure in full view.

Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times

Re: bit of maths help please

Post by Steve001 »

It's ok think i have sussed it . . .

steve
Success always occurs in private and failure in full view.

Post Reply