Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > PORTA & 0x03 == 0x03

Reply
Thread Tools

PORTA & 0x03 == 0x03

 
 
sonos
Guest
Posts: n/a
 
      07-12-2006
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
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      07-12-2006
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.

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      07-12-2006
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)
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      07-12-2006
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
 
Reply With Quote
 
Thad Smith
Guest
Posts: n/a
 
      07-12-2006
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
 
Reply With Quote
 
Fred Kleinschmidt
Guest
Posts: n/a
 
      07-12-2006

"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


 
Reply With Quote
 
Tom St Denis
Guest
Posts: n/a
 
      07-12-2006
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

 
Reply With Quote
 
Tom St Denis
Guest
Posts: n/a
 
      07-12-2006
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

 
Reply With Quote
 
Giorgio Silvestri
Guest
Posts: n/a
 
      07-12-2006

"Tom St Denis" <> ha scritto nel messaggio
news: 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




 
Reply With Quote
 
Gernot Frisch
Guest
Posts: n/a
 
      07-13-2006

"sonos" <> schrieb im Newsbeitrag
news:d5ydnXaea-...
> 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) ...


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Traffico porta monitor marco74 Cisco 1 10-20-2010 03:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57