View Full Version : Masking bits with the and operator...

03-21-2009, 11:03 AM
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:







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

Mike Green
03-21-2009, 11:31 AM
There are two "and" operators. One is for logical expressions (AND) and the other is for bits (&). You need to use the latter.

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.

03-25-2009, 06:08 AM
Hey Joms,

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
Its a 0

You may have already gotten to the bottom of this, just thought I'd relate my recently found solution.


Adrian Schneider
03-25-2009, 02:06 PM
I think you mean something like

if ((variable & |< bitNumber) <> 0)
'-- bit is set

03-25-2009, 02:50 PM
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

03-25-2009, 03:16 PM
Bits are counted from right to left, starting with 0.
i.e. 76543210 (not 87654321)

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.

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.

A nice and neat solution !

03-25-2009, 08:45 PM
I was going by Joms's layout of 87654321, and I was going for a conseptual explanation anyway, but thank you.

That does test for a single bit, I'm checking the shifted result for >128 not the original value.

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.


03-25-2009, 09:14 PM
@CassLan: Yep ... sorry about my mistake. I missed the byte thing. And please don't be sour because someone tries to correct a mistake in an helping post but is mistaken by himself. Making mistakes is usual to mankind. As long as everybody is willing to find the truth and thinks about other opinions there is no problem. And fortunately we discuss in an area where truth simply can be proven.

03-25-2009, 09:41 PM
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.

03-25-2009, 09:57 PM
@MagIO2 and nohab:
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ˇ http://forums.parallax.com/images/smilies/lol.gif

where did I put my coffee....


03-25-2009, 10:30 PM
Sometimes it's hard to be helpful. Analyzing a problem with not enough information as happens sometimes is difficult. Providing a nicely thought out answer is difficult. Often the issue gets totally misunderstood or someone's special agenda pushes answers that are probably irrelevant. Just espousing some fundamental is also not very helpful most times. It is easier to pick apart a concrete problem than provide a generic analysis. When things are misinterpreted it's easy to get frustrated. We all see things so differently and there is no immediate feedback system on the forums. Patience and "charitable" reading go a long way here.


Propalyzer: Propeller PC Logic Analyzer (http://www.brouhaha.com/~sdenson/Propalyzer)
http://forums.parallax.com/showthread.php?p=788230 (http://forums.parallax.com/showthread.php?p=788230)