Created
March 5, 2014 01:34
-
-
Save naquad/9359520 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __BIT_OPS__ | |
#define __BIT_OPS__ | |
#ifndef _BV | |
#define _BV(x) (1 << (x)) | |
#endif | |
/* #!ruby | |
* | |
* # generator for bitN macroes | |
* | |
* ARGC = 8 | |
* | |
* def seqfmt(s, e, fmt, join = ', ') | |
* ret = s.send(s > e ? :downto : :upto, e).map {|x| fmt % x} | |
* join ? ret.join(join) : ret | |
* end | |
* | |
* 1.upto(ARGC).each do |n| | |
* puts "#define bits#{n}(#{seqfmt 1, n, 'bit%d'}) (#{seqfmt 1, n, '_BV(bit%d)', ' | '})" | |
* end | |
* | |
* puts <<EOF | |
* | |
* #define __bits__get_macro(#{seqfmt(1, ARGC, '_%d')}, macro_name, ...) macro_name | |
* #define bits(...) __bits__get_macro(__VA_ARGS__, #{seqfmt(ARGC, 1, 'bits%d')})(__VA_ARGS__) | |
* | |
* EOF | |
*/ | |
#define bits1(bit1) (_BV(bit1)) | |
#define bits2(bit1, bit2) (_BV(bit1) | _BV(bit2)) | |
#define bits3(bit1, bit2, bit3) (_BV(bit1) | _BV(bit2) | _BV(bit3)) | |
#define bits4(bit1, bit2, bit3, bit4) (_BV(bit1) | _BV(bit2) | _BV(bit3) | _BV(bit4)) | |
#define bits5(bit1, bit2, bit3, bit4, bit5) (_BV(bit1) | _BV(bit2) | _BV(bit3) | _BV(bit4) | _BV(bit5)) | |
#define bits6(bit1, bit2, bit3, bit4, bit5, bit6) (_BV(bit1) | _BV(bit2) | _BV(bit3) | _BV(bit4) | _BV(bit5) | _BV(bit6)) | |
#define bits7(bit1, bit2, bit3, bit4, bit5, bit6, bit7) (_BV(bit1) | _BV(bit2) | _BV(bit3) | _BV(bit4) | _BV(bit5) | _BV(bit6) | _BV(bit7)) | |
#define bits8(bit1, bit2, bit3, bit4, bit5, bit6, bit7, bit8) (_BV(bit1) | _BV(bit2) | _BV(bit3) | _BV(bit4) | _BV(bit5) | _BV(bit6) | _BV(bit7) | _BV(bit8)) | |
#define __bits__get_macro(_1, _2, _3, _4, _5, _6, _7, _8, macro_name, ...) macro_name | |
#define bits(...) __bits__get_macro(__VA_ARGS__, bits8, bits7, bits6, bits5, bits4, bits3, bits2, bits1)(__VA_ARGS__) | |
#define set_bits(what, ...) (what = bits(__VA_ARGS__)) | |
#define enable_bits(what, ...) (what |= bits(__VA_ARGS__)) | |
#define disable_bits(what, ...) (what &= ~bits(__VA_ARGS__)) | |
#define toggle_bits(what, ...) (what ^= bits(__VA_ARGS__)) | |
#define set_bit(x, y) set_bits(x, y) | |
#define enable_bit(x, y) enable_bits(x, y) | |
#define disable_bit(x, y) disable_bits(x, y) | |
#define toggle_bit(x, y) toggle_bits(x, y) | |
/** | |
* Example usage: | |
* | |
* set_bits(DDRB, PB0, PB1, PB5); => DDRV = _BV(PBO) | _BV(PB1) | _BV(PB5); | |
* | |
* enable_bits(DDRB, PB0, PB1, PB5); => DDRV |= _BV(PBO) | _BV(PB1) | _BV(PB5); | |
* | |
* disable_bits(DDRB, PB0, PB1, PB5); => DDRV &= ~(_BV(PBO) | _BV(PB1) | _BV(PB5)); | |
* | |
* toggle_bits(DDRB, PB0, PB1, PB5); => DDRV ^= _BV(PBO) | _BV(PB1) | _BV(PB5); | |
* | |
*/ | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment