Here's a macro that Mathew Hendry posted back in the year 2000 for
achieving binary integer literals that evaluate to compile-time
constants:
#define BIN8(n)\
(((0x##n##ul&1< < 0)>0)|((0x##n## ul&1<< 4)>3)\
|((0x##n##ul&1< < 8)>6)|((0x##n## ul&1<<12)>9)\
|((0x##n##ul&1< <16)>>12)|((0x# #n##ul&1<<20)>> 15)\
|((0x##n##ul&1< <24)>>18)|((0x# #n##ul&1<<28)>> 21))
Now admittedly I don't know how it works mathematically, but still I
want to perfect it. The first thing I did was made it more readable
(in my own opinion of course):
#define BIN8(n)\
(
((0x##n##ul & 1<<0) >0) | ((0x##n##ul & 1<<0) >>
3) \
| ((0x##n##ul & 1<<8) >6) | ((0x##n##ul & 1<<12)>>
9) \
| ((0x##n##ul & 1<<16)>>12) | ((0x##n##ul &
1<<20)>>15) \
| ((0x##n##ul & 1<<24)>>18) | ((0x##n##ul &
1<<28)>>21) \
)
From there, the only flaw I can see is in the expression "1 << 24",
which I think should be "1lu << 24", so that gives us:
#define BIN8(n)\
(
((0x##n##ul & 1lu<<0) >0) | ((0x##n##ul & 1lu<<0)
>3) \
1lu<<12)>9) \
| ((0x##n##ul & 1lu<<16)>>12) | ((0x##n##ul &
1lu<<20)>>15) \
| ((0x##n##ul & 1lu<<24)>>18) | ((0x##n##ul &
1lu<<28)>>21) \
)
Is that perfect now? Or does it need more tweaking? Even if you post
to say you think it's perfect then that'd be a help.
Comment