c++ - How to generate set of enumerations with even number of ones -
i have idea of how prevent single bit flip (due cosmic radiation or similar externally induced event) causing enumerations (enum) change 1 defined value defined value in relatively easy way. put simple each value should have amount of ones, binary speaking. if 1 flips, enum odd , guaranteed not match other enum.
i'm not sure how "generate" such sequence may used enum values values must compile time constant. macro function returning n:th element in set perfectly.
the first few numbers in sequence 0 (000), 3 (011), 5 (101), 6 (110). think idea now.
non-enumeration (non-compile time) answers appreciated may me realize how myself.
to make myself clear want macro generating n:th number in enum number of ones in bit pattern, similar macros generating fibbonachi sequence. lowest bit parity bit.
most of memory protected hardware ecc, l1 cache being 1 exception. single bit error in l1 has been measured occur once every 10000h enough seen requirements.
vram not protected. there have rgb(a) raster, few general purpose raster (like stencil) , geometry. rgb raster rather unsensative bit flips used visualization. erroneous geometry in general visible, rare (few kb) , design resolved user induced reboot.
for 4096x4096x8bit stencil (~16mb) single bit error rate in environment once every 8h average cosmic radiation, more during solar storms. not bad in opinion, i'd hate filling paper work proving officers why fine in application , elses using stencil data without regard how data used. if having parity bit in stencil value i'd able detect errors , if necessary re-generate stencil hoping better results. stencil can generated in less second risk of errors occuring twice in row considered low.
so basically, generating set of enumerations parity dodge bullet of current , future paper work , research regarding how may affect app , others'.
if want know if enum either valid or if bit flipped, can use values , parity bit makes total number of bits even. (which first sequence identical example)
0000000 0 = 0 0000001 1 = 3 0000010 1 = 5 0000011 0 = 6 0000100 1 = 9 0000101 0 = 10 0000110 0 = 12 0000111 1 = 15
which can done by
int encode_enum(int e) { return (e << 1) + (number_of_bits_set(e) % 2); }
however, if want able restore value, simple way duplication; have multiple copies of value can later compared eachother. you'd need 3 copies restore it. if list of values small, can encode 1 integer.
int encode_enum(int e) { return (e << 20) | (e << 10) | e; }
which if e
less 2^10
copied 3 times single 32-bit integer.
Comments
Post a Comment