G'day all! I am looking for a way to determine if a byte value is divisible by 3 and was about to appeal for help when I remembered that Andy Warren had started a discussion on that very topic about a year ago. I went looking in my mail archives and found something that looked quite good written by John Payson. I tried the shorter of the 2 routines that John posted and found problems with it. The longer (but faster) routine worked just fine. I spent a couple of hours with the problem routine and made changes which seem to work just fine. Some really neat concepts came out of that discussion that Andy started back then. I learned some really basic math stuff (casting out nines) that I had never seen before. Thank you to everyone who contributed. Here is John's shorter routine: >> Sum up the 4 "digits": 00 + 10 + 00 + 01 = 11b >> which of course is divisible by three. >Actually, I think it's easier to observe that 16%3=1 and do something like >either this: swapf Number,w addwf Number,f ; Note [MSN of Number] % 3 == old Number % 3 rrf Number,w ; We want to add what are now upper and lower rlf Number,f ; two bits of MSN; 00 or 11 would be good. bsf Number,4 ; By setting prev two bit of both addends, iorlw $10 ; we turn 00 and 11 into 01 and 00. addwf Number,f ; Now we're interested in Number:6 btfss Number,6 ; If set, not multiple of three retlw 0 ; Return "nope" retlw 1 ; Return "yep" >11 cycles constant execution time in 10 words, including the retlw. The above routine works on some values, but fails on others (at least when simulated on MPLAB-SIM 3.22.02). Single stepping through the problem values showed that setting bit 4 in both addends so as to force a carry to bit 5 was the problem: sometimes adding both bits was already going to cause a carry to bit 5 and thus bit 5 needed to be incremented by 2, but was only incremented by 1. I'm not sure this makes sense; single step through the code and you will see what I mean. John is testing to see if the final bit pair (bits 5&6) is 11. If it is 11, then the original number is divisible by 3. I simply modified the code to add 1 directly instead of relying on a forced carry from bit 4. It is the same length and requires the same number of cycles. swapf Number,w ; split #, add 2 halves, keep Most Sig Nybble addwf Number,f ; Note [MSN of Number] % 3 == old Number % 3 rrf Number,w ; We want to add what are now upper and lower rlf Number,f ; two bits of MSN; 00 or 11 would be good. addwf Number,w ; If bits 6&7 are 1, # is divisible by 3 addlw b'00100000' ; treat bits 6&7 as 2 bit #, increment andlw b'01000000' skpz retlw 0 ; Return "nope" retlw 1 ; Return "yep" My thanks again for all the help that I have recieved from piclist members. Dwayne Dwayne Reid Trinity Electronics Systems Ltd Edmonton, Alberta, CANADA (403) 489-3199 voice (403) 487-6397 fax