By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

PORTA & 0x03 == 0x03

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a

"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.