bit shifting

  • Follow


Hello

I'm working on the code for the embedded platform, where I need to obtain a 
value for on of 32 GPIO pins. On this platform sizeof(unsigned int) is 32, 
so I've come up with such macro:

#define GPIO(x)    (0x1u << (x))

Is this fine or has any disadvantages!

-- 
Mark 

0
Reply Mark 12/30/2009 8:25:24 AM

"Mark" <mark_cruzNOTFORSPAM@hotmail.com> writes:

> I'm working on the code for the embedded platform, where I need to
> obtain a value for on of 32 GPIO pins. On this platform
> sizeof(unsigned int) is 32,

I think you mean sizeof(unsigned int) is 4.  By the way, a lot of
people quote sizes when the quantity that matters is really the
maximum for that type.  On some C implementations, sizeof(int) can be
1 yet MAX_INT can be a respectable 2147483647.  The simplest thing
would be for you to say that, on your machine, unsigned int has 32
value bits.

> so I've come up with such macro:
>
> #define GPIO(x)    (0x1u << (x))
>
> Is this fine or has any disadvantages!

I think it is fine.  I'd write 1u rather than a hex constant but that
is just my taste.  All macros have some disadvantages, but you use all
caps so the reader will know that GPIO is not a function.

-- 
Ben.
0
Reply Ben 12/30/2009 11:44:39 AM


1 Replies
329 Views

(page loaded in 0.04 seconds)

Similiar Articles:













7/23/2012 7:34:06 PM


Reply: