c++ - 8-digit BCD check -
i've 8-digit bcd number , need check out see if valid bcd number. how can programmatically (c/c++) make this?
ex: 0x12345678 valid, 0x00f00abc isn't.
thanks in advance!
you need check each 4-bit quantity make sure it's less 10. efficiency want work on many bits can @ single time.
here break digits apart leave 0 between each one, add 6 each , check overflow.
uint32_t highs = (value & 0xf0f0f0f0) >> 4; uint32_t lows = value & 0x0f0f0f0f; bool invalid = (((highs + 0x06060606) | (lows + 0x06060606)) & 0xf0f0f0f0) != 0;
edit: can better. doesn't take 4 bits detect overflow, 1. if divide digits 2, frees bit , can check digits @ once.
uint32_t halfdigits = (value >> 1) & 0x77777777; bool invalid = ((halfdigits + 0x33333333) & 0x88888888) != 0;
Comments
Post a Comment