[Top][All Lists]

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

Re: [avr-gcc-list] How to (efficeiently !!!) test a bit within a multi-b

From: David Kelly
Subject: Re: [avr-gcc-list] How to (efficeiently !!!) test a bit within a multi-byte integer ?
Date: Fri, 4 Nov 2005 07:37:09 -0600

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

reply via email to

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