avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [avr-gcc-list] How to (efficeiently !!!) test a bit within amulti-by


From: niklo
Subject: RE: [avr-gcc-list] How to (efficeiently !!!) test a bit within amulti-byte integer ?
Date: Fri, 4 Nov 2005 15:54:24 +0100

Great! 
But you still have to use the correct operator:
&  is bit-wise and
&& is logical and

You probably want "&" otherwise why compare with 0x04?

/niklo

-----Original Message-----
From: address@hidden
[mailto:address@hidden On Behalf Of David
Kelly
Sent: den 4 november 2005 14:37
To: Vincent Trouilliez
Cc: avr-gcc
Subject: Re: [avr-gcc-list] How to (efficeiently !!!) test a bit within
amulti-byte integer ?


On Nov 3, 2005, at 11:35 PM, Vincent Trouilliez wrote:
>
> static void log_address_set( uint32_t address )
> {
> ...
> ...
>       if (address & 0x00040000)
>               PORTB |= LOG_SDA;
>       else
>               PORTB &= ~LOG_SDA;
> ...
> }


On Nov 4, 2005, at 1:20 AM, Ian Caddy wrote:
>
> I haven't tried this, but it might be better:
>
> unsigned char temp;
>
> temp = (address >> 16) & 0xFF;
>
> if(temp & 0x04)
> {
>    /* Your code */
> }

In other messages & has been replaced with &&.

Ian is close to how I would have handled it. I use a "union32_t" type  
a fair bit in my code for no other purpose than to pluck values out  
of the middle painlessly.

typedef union union32_t {
        struct __attribute__ ((packed)) {
                uint8_t a, b, c, d;
        } u8;

        struct __attribute__ ((packed)) {
                uint8_t a;
                uint16_t bc;
                uint8_t d;
        } mixed;

        struct __attribute__ ((packed)) {
                uint16_t ab, cd;
        } u16;

        uint32_t u32;
};

union32_t address;

if( address.u8.c && 0x04 )
...

avr-gcc does the expected without "__attribute__ ((packed))" but if  
you get to playing in Linux or FreeBSD its needed to do the same as  
what avr-gcc does.

Also note the above is sensitive to machine endianess. Macintosh,  
Sun, and Irix are big-endian.

--
David Kelly N4HHE, address@hidden
========================================================================
Whom computers would destroy, they must first drive mad.



_______________________________________________
AVR-GCC-list mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/avr-gcc-list





reply via email to

[Prev in Thread] Current Thread [Next in Thread]