Specific bit
bpislife
Posts: 10
Is there anyway to check to see if a particular bit is a "1" using SPIN?
In other words, I have a byte of data and want to check to see if the 3rd bit is a "1".
x = don't care
xxxxx1xx
I know how I would do this on a PC the old fashion way by using the VB left and VB right command until I have trimmed off all the bits I don't need but I am not sure how to do this with SPIN.
In other words, I have a byte of data and want to check to see if the 3rd bit is a "1".
x = don't care
xxxxx1xx
I know how I would do this on a PC the old fashion way by using the VB left and VB right command until I have trimmed off all the bits I don't need but I am not sure how to do this with SPIN.
Comments
Thanks!!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon McPhalen
Hollywood, CA
Here is a link to a diagram of an AND gate...
http://www.ee.surrey.ac.uk/Projects/Labview/gatesfunc/GateMain.htm
...on the left are two inputs. On the right one output...
If your inputs are 1 and 0 (one and zero), the output will be 0 (zero).
Or 0 and 1 = 0 as well.
But if the inputs are 1 and 1, then the output would be 1.
So just using that·AND gate and·a single digit Mystery number, we could·AND that single digit number with another number. Or with what is called a·Mask. A mask is·just a number or a string or numbers.
So let's set the mask to 1, then AND it with the Mystery number...
ANSWER: The output is a 1!
What is our Mystery number? It is a 1.
You can AND the upper row with the bottom row...
10101011
00000001 (mask)
output = 1
The mask does not allow any numbers to be a 1 except the last digit because they are all 0's except the last digit...
That makes perfect sense now.
Thanks everyone for you inputs.
pub getbit(target, pos)
return (target >> pos) & 1
As that's only doing a single shift, plus the AND of a constant.
To explain further, it shifts the bit position you're interested in down to the 1st bit, then ANDs off all bits but the lowest one.
Jason
This doesn't look right.
Like on page 70 of the Propeller Education Kit Labs pdf...
·
Jason was posting an alteration to Jonny's methods (you could use those methods in your code if you wanted)
You could use Jonny's get bit routine like this:
or not using the method:
Graham
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon McPhalen
Hollywood, CA