Masking bits with the and operator...
Joms
Posts: 279
Stefan has explained to me that I can use masking to cover up unused bits... I have a 8bits together in a byte and I want to know the status of each bit seperatly.
I didn't understand how to mask things, but I thought I had it working by shifting the bits to the left, checking the current bit, shifting again, and so on. Turns out if I had more then one bit active it does not work...
This is the example Stefan provided:
bitno···87654321
value··%10101000
bitno···87654321
Mask···%00001000
value·and·Mask
value··%10101000
Mask···%00001000
result·%00001000
value··%10101000
Mask···%00000001
result·%00000000
I tried making the following:
CON·· mask = %00000001
result = (variable here) AND mask
My question... Because their is not an operator called 'mask' how can this be done. I attempted to make constants and 'AND' them together with my variable, but that ended up leaving me with nothing unless only the one of the bytes was active. Is there a command for this, or its it best to write the code out the long way with several shift/recheck repeats?
If there is not a direct command, how do you tell the status of only the left most bit of a byte? I do not care about the rest as I will shift them over one by one, checking each time?
(if you are wondering what I am doing, I am trying to use a 74165 to input 8 different buttons. I already wrote code to scan and load the byte into a var, just having a problems using the byte)
Thanks for any help... I am not looking for someone to write the code for me, but just tell me where to start as most things I have tried are not working... Thanks...
Post Edited (Joms) : 3/21/2009 4:16:21 AM GMT
I didn't understand how to mask things, but I thought I had it working by shifting the bits to the left, checking the current bit, shifting again, and so on. Turns out if I had more then one bit active it does not work...
This is the example Stefan provided:
bitno···87654321
value··%10101000
bitno···87654321
Mask···%00001000
value·and·Mask
value··%10101000
Mask···%00001000
result·%00001000
value··%10101000
Mask···%00000001
result·%00000000
I tried making the following:
CON·· mask = %00000001
result = (variable here) AND mask
My question... Because their is not an operator called 'mask' how can this be done. I attempted to make constants and 'AND' them together with my variable, but that ended up leaving me with nothing unless only the one of the bytes was active. Is there a command for this, or its it best to write the code out the long way with several shift/recheck repeats?
If there is not a direct command, how do you tell the status of only the left most bit of a byte? I do not care about the rest as I will shift them over one by one, checking each time?
(if you are wondering what I am doing, I am trying to use a 74165 to input 8 different buttons. I already wrote code to scan and load the byte into a var, just having a problems using the byte)
Thanks for any help... I am not looking for someone to write the code for me, but just tell me where to start as most things I have tried are not working... Thanks...
Post Edited (Joms) : 3/21/2009 4:16:21 AM GMT
Comments
There are two assignment operators. One is for constant expressions (=) and the other is for variables (:=). You need to use the latter with "result".
Shift operators are also very useful, mostly << and >>. The bitmask operator is also helpful (|<) for single bit masks. Look up the descriptions and examples in the Propeller Manual.
I resently had a similar need and did the following:
assuming that myvar is the byte in question, and I wanted to know if the 4th bit is 1 or 0;
temp := myvar << 4
if temp >= 128
Its a 1
else
Its a 0
You may have already gotten to the bottom of this, just thought I'd relate my recently found solution.
Rick
That's not a test for a single bit. If the 5th bit is set and the 4th is not set your result will be bigger then 128 either! So your test only works if bits 5 to 7 are definitely 0.
In general when talking about bits you talk about a range of 0 to 7 for bytes, 0 to 15 for words and so on. That's because of the power of two representation:
2^0 = 1 = %00000001
2^1 = 2 = %00000010
2^2 = 4 = %00000100
and so on
Bits are counted from right to left, starting with 0.
i.e. 76543210 (not 87654321)
@CassLan:
Having a byte with the 4th bit set and nothing else would be %00010000
Shifting this 4 steps left with give an empty byte.
I assume you mean the 3rd bit ?! then your example will work.
@MagIO2:
CassLan is shifting left 4, assuming myvar is defined as a byte, the bits 7,6,5 and 4 will be shifted out in space, leaving the 3rd bit in the leftmost position.
Comparing with 128 will tell if the originally 3rd bit is 0 or 1.
@Adrian:
A nice and neat solution !
I was going by Joms's layout of 87654321, and I was going for a conseptual explanation anyway, but thank you.
MagIO2:
That does test for a single bit, I'm checking the shifted result for >128 not the original value.
Adrian:
That's awesome!
I hate to be a little sour but this post was up for 4 days with only 1 response...it seems like people are much more inclined to post a correction to someone trying to help than actually helping.
Maybe it's just coincidence.
Rick
Sorry if I made you sour!
I just wanted to correct the statement from MagIO2, but then I also had to adjust Jom and you to get it all correct. The bit numbering sometimes cause misinterpretations that could be difficult to sort out.
I'm having a rough morning....(stayed up very late with the prop!!). Your both gentlemen for replying.
I need to lighten up a bit·
where did I put my coffee....
Rick
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
Propalyzer: Propeller PC Logic Analyzer
http://forums.parallax.com/showthread.php?p=788230