Let's put this into perspective, The OP wanted ideas on how to code a particular type of function in an efficient way, this contained 4 bits (possibly expanding to 8 bits), some solutions have been put forward, however, it seems yet again it's turning into a contest of who can write the function in the least amount of code, all the what if etc... Yes I agree it's good to submit different ideas and possibly enhance on those ideas, it may not particularly help the OP any more but will give others looking through these posts ideas going forward, what seems to be happening again is a slogging match between members (yes I probably find myself being caught up in it as well). Programmers tend to be highly strung and it sure shows, I have been in this industry for over 35 years, do not claim to be the best, however, I have shown to be able to do my job right, have extensive experience in all areas of controls and done work for many well known blue chip companies. I have worked closely with production teams and done extensive training for plant engineers. I have great respect for many of the members here people like Peter N, Ron B and a number of others. Keep the ideas rolling in but let's keep it in context.