468,456 Members | 1,647 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,456 developers. It's quick & easy.

PORTA & 0x03 == 0x03

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
Jul 12 '06 #1
9 2671
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
Jul 12 '06 #2
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.

Jul 12 '06 #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)
Jul 12 '06 #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
Jul 12 '06 #5

"Chris Dollin" <ch**********@hp.comwrote in message
news:e9**********@malatesta.hpl.hp.com...
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
Jul 12 '06 #6
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

Jul 12 '06 #7
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

Jul 12 '06 #8

"Tom St Denis" <to********@gmail.comha scritto nel messaggio
news:11**********************@35g2000cwc.googlegro ups.com...
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


Jul 12 '06 #9

"sonos" <so***@nospam.comschrieb im Newsbeitrag
news:d5******************************@giganews.com ...
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) ...
Jul 13 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Thomas Scheffler | last post: by
14 posts views Thread by Arne | last post: by
7 posts views Thread by John Nagle | last post: by
1 post views Thread by Richard Eich | last post: by
reply views Thread by sopra_sotto | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.