PORTA & 0x03 == 0x03

S

sonos

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
 
C

Chris Dollin

Thad said:
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).
 
R

Richard Heathfield

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

Frederick Gotham

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 ))
 
T

Thad Smith

sonos said:
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) ...
 
F

Fred Kleinschmidt

Chris Dollin said:
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.
 
T

Tom St Denis

Fred said:
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
 
T

Tom St Denis

Tom said:
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
 
G

Giorgio Silvestri

Tom St Denis said:
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
 
G

Gernot Frisch

sonos said:
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) ...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top