PORTA & 0x03 == 0x03

Discussion in 'C Programming' started by sonos, Jul 12, 2006.

  1. sonos

    sonos Guest

    Hi,
    I hope this is the right usenet group for posting this code...

    IF (PORTA & 0x03 == 0x03) ...

    ... I want to verify if PORTA (0x00 to 0xFF) has bit 1 and bit 2 set to 1.

    Is this the proper code?

    Thanks
    sonos, Jul 12, 2006
    #1
    1. Advertising

  2. sonos

    Chris Dollin Guest

    Thad Smith wrote:

    > sonos wrote:
    >
    >> I hope this is the right usenet group for posting this code...
    >>
    >> IF (PORTA & 0x03 == 0x03) ...
    >>
    >> .. I want to verify if PORTA (0x00 to 0xFF) has bit 1 and bit 2 set to 1.
    >>
    >> Is this the proper code?

    >
    > No. "if" must be lowercase. You must parenthesize to get the operation
    > you want:
    >
    > if ((PORTA & 0x03) == 0x03) ...


    And you don't /need/ to write the number in hex:

    if ((PORTA & 3) == 3) ...

    although your style may prefer it (especially if there are wider bit-patterns
    around).

    --
    Chris "seeker" Dollin
    A rock is not a fact. A rock is a rock.
    Chris Dollin, Jul 12, 2006
    #2
    1. Advertising

  3. sonos said:

    > Hi,
    > I hope this is the right usenet group for posting this code...
    >
    > IF (PORTA & 0x03 == 0x03) ...


    ....looks oddly familiar. The same expression - and I mean precisely the same
    expression - appeared in an OP a week or three ago.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Jul 12, 2006
    #3
  4. sonos posted:

    > Hi,
    > I hope this is the right usenet group for posting this code...
    >
    > IF (PORTA & 0x03 == 0x03) ...
    >
    > .. I want to verify if PORTA (0x00 to 0xFF) has bit 1 and bit 2 set to 1.
    >
    > Is this the proper code?



    No no on!

    Open up your favourite operator precedence table:

    http://www.difranco.net/cop2220/op-prec.htm

    You'll notice that "==" has higher precedence than "&", so you'll need
    parentheses.

    if(3 == (PORTA & 3 ))

    --

    Frederick Gotham
    Frederick Gotham, Jul 12, 2006
    #4
  5. sonos

    Thad Smith Guest

    sonos wrote:

    > I hope this is the right usenet group for posting this code...
    >
    > IF (PORTA & 0x03 == 0x03) ...
    >
    > .. I want to verify if PORTA (0x00 to 0xFF) has bit 1 and bit 2 set to 1.
    >
    > Is this the proper code?


    No. "if" must be lowercase. You must parenthesize to get the operation
    you want:

    if ((PORTA & 0x03) == 0x03) ...

    --
    Thad
    Thad Smith, Jul 12, 2006
    #5
  6. "Chris Dollin" <> wrote in message
    news:e92s1q$a2q$...
    > Thad Smith wrote:
    >
    >> sonos wrote:
    >>
    >>> I hope this is the right usenet group for posting this code...
    >>>
    >>> IF (PORTA & 0x03 == 0x03) ...
    >>>
    >>> .. I want to verify if PORTA (0x00 to 0xFF) has bit 1 and bit 2 set to
    >>> 1.
    >>>
    >>> Is this the proper code?

    >>
    >> No. "if" must be lowercase. You must parenthesize to get the operation
    >> you want:
    >>
    >> if ((PORTA & 0x03) == 0x03) ...

    >
    > And you don't /need/ to write the number in hex:
    >
    > if ((PORTA & 3) == 3) ...
    >
    > although your style may prefer it (especially if there are wider
    > bit-patterns
    > around).


    The OP said:
    I want to verify if PORTA (0x00 to 0xFF) has bit 1 and bit 2 set to 1.

    One could imply from this that the correct answer would be:
    if ( PORTA & 3 ) ...
    because the OP did not specify that ONLY bits 1 and 2 must be set.

    >
    > --
    > Chris "seeker" Dollin
    > A rock is not a fact. A rock is a rock.
    >

    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Software Reuse Project
    Fred Kleinschmidt, Jul 12, 2006
    #6
  7. sonos

    Tom St Denis Guest

    Fred Kleinschmidt wrote:
    > The OP said:
    > I want to verify if PORTA (0x00 to 0xFF) has bit 1 and bit 2 set to 1.
    >
    > One could imply from this that the correct answer would be:
    > if ( PORTA & 3 ) ...
    > because the OP did not specify that ONLY bits 1 and 2 must be set.



    I think you had a brain fart. a & 3 will be true if a & 1 is true
    and/or if a & 2 is true.

    The two cases are

    if ((a & 3) == a) { ... }

    which means ONLY both bits are set and no others or

    if ((a & 3) == 3) { ... }

    which means that the two lower bits must be set

    Tom
    Tom St Denis, Jul 12, 2006
    #7
  8. sonos

    Tom St Denis Guest

    Tom St Denis wrote:
    > if ((a & 3) == a) { ... }
    >
    > which means ONLY both bits are set and no others or


    foot planted in mouth...

    ....

    if (a == 3) { ... }

    Teach me to be all superior. hehehehehe

    Tom
    Tom St Denis, Jul 12, 2006
    #8
  9. "Tom St Denis" <> ha scritto nel messaggio
    news:...
    > Tom St Denis wrote:
    > > if ((a & 3) == a) { ... }
    > >
    > > which means ONLY both bits are set and no others or

    >
    > foot planted in mouth...
    >
    > ...
    >
    > if (a == 3) { ... }
    >
    > Teach me to be all superior. hehehehehe
    >
    > Tom
    >


    Not completely equivalent.

    I presume 'a' and 'PORTA', see the OP question, are
    device registers, probably declared with volatile.

    Consequently

    if (a == 3) {
    /* ... */
    }

    is different from

    if ((a & 3) == a) {
    /* ... */
    }

    In second form the memory-mapped I/O port is read twice.
    It can be important.


    Giorgio Silvestri
    Giorgio Silvestri, Jul 12, 2006
    #9
  10. "sonos" <> schrieb im Newsbeitrag
    news:...
    > Hi,
    > I hope this is the right usenet group for posting this code...
    >
    > IF (PORTA & 0x03 == 0x03) ...
    >
    > .. I want to verify if PORTA (0x00 to 0xFF) has bit 1 and bit 2 set
    > to 1.
    >
    > Is this the proper code?


    == comes before &, thus you want:
    if ( (PORTA&0x03) == 0x03) ...
    Gernot Frisch, Jul 13, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page