Shop OBEX P1 Docs P2 Docs Learn Events
Masking bits with the and operator... — Parallax Forums

Masking bits with the and operator...

JomsJoms Posts: 279
edited 2009-03-25 15:30 in Propeller 1
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

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-03-21 04:31
    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.
  • CassLanCassLan Posts: 586
    edited 2009-03-24 23:08
    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
    else
    Its a 0

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

    Rick
  • Adrian SchneiderAdrian Schneider Posts: 92
    edited 2009-03-25 07:06
    I think you mean something like
    if ((variable & |< bitNumber) <> 0)
      '-- bit is set
    
    
    
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-03-25 07:50
    @CassLan:
    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
  • nohabnohab Posts: 96
    edited 2009-03-25 08:16
    @Joms:
    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 !
  • CassLanCassLan Posts: 586
    edited 2009-03-25 13:45
    nohab:
    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
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-03-25 14:14
    @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.
  • nohabnohab Posts: 96
    edited 2009-03-25 14:41
    @CassLan:
    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.
  • CassLanCassLan Posts: 586
    edited 2009-03-25 14:57
    @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· lol.gif

    where did I put my coffee....

    Rick
  • jazzedjazzed Posts: 11,803
    edited 2009-03-25 15:30
    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.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve


    Propalyzer: Propeller PC Logic Analyzer
    http://forums.parallax.com/showthread.php?p=788230
Sign In or Register to comment.