Incrementing time represented as NBCD format - Printable Version +- Qbasicnews.com (http://qbasicnews.com/newforum) +-- Forum: QbasicNews.Com (http://qbasicnews.com/newforum/forum-3.html) +--- Forum: Challenges (http://qbasicnews.com/newforum/forum-10.html) +--- Thread: Incrementing time represented as NBCD format (/thread-9794.html) |
Incrementing time represented as NBCD format - Blitz - 12-05-2006 NBCD stands for Natural Binary Coded Decimal and basically means that you store decimal numbers as you naturally would on paper, except as binary. So what does this mean? It means you use 4 bits to store one number, 0-9. And store them as the ordinary decimal positioning system. So the hexdecimal number 0x3459 stands for the decimal number 3459 and not what it actually means in binary (which is 13401). In this case we're storing time as minutes and seconds in the NBCD format. For example, 0x4712 means 47 minutes and 12 seconds. So the challenge is to write the most elegant code (in which ever language prefer) you can to increment an integer representing time (only minutes and seconds). You have to makes sure that 0x5959 wraps back to 0 once incremented. Challenge closes in exactly one week. Good luck Incrementing time represented as NBCD format - 1000101 - 12-06-2006 FreeBasic code (tested with Win32 CVS Dec 5, 2006) Code: #Define HexPad( Number, Padding ) ( Right( String( Padding, "0" ) & Hex( Number ), Padding ) ) Incrementing time represented as NBCD format - yetifoot - 12-06-2006 Thats a pretty nice solution by 1000101 there, I would have done it a similar way, but in order to have something to show, I decided to do it the following way. Again FreeBASIC, Code: Function inc_nbcd_timestamp(ByVal timestamp As Integer) As Integer Incrementing time represented as NBCD format - Blitz - 12-06-2006 Here's my best shot Code: function nbcdTimeIncr ( byval t as integer ) as integer Add AND &hffff if you care that the upper 16 bits are zero or not. Incrementing time represented as NBCD format - yetifoot - 12-06-2006 Very interesting, I'm still trying to understand exactly how it works, but I think I'm getting there. I managed to spot a couple of reductions: Code: function inc_nbcd_timestamp ( byval t as integer ) as integer Incrementing time represented as NBCD format - LooseCaboose - 12-06-2006 Its easy in Ada: Code: procedure add is Incrementing time represented as NBCD format - Dio - 12-06-2006 i think i misunderstood. Code: ? mid$(time$,4,2)+right$(time$,2):sleep Incrementing time represented as NBCD format - Blitz - 12-06-2006 Loose, that's nice. Though that's not really in the nbcd format is it? Dio, i think you missunderstood. It's not a matter of getting the time as a string. yetifoot, ic. Didn't know eqv was defined as exactly that. I didn't actually write it in FB. It was originally in assembler. It works by incrementing the number by one, then adding the correct bits so that the numbers which has gone past past 9 and 6 overflow to the next number and wrap around. Incrementing time represented as NBCD format - yetifoot - 12-07-2006 I had a play around in assembler too, i thought the BCD functions might be useful, DAA in particular, using some carry trickery. I was hoping to get a code in only 4 or 5 instructions, but the best I could do was 11 including the moves to/from timestamp, and that wasn't using the x86 BCD instructions at all, it was just a quick reworking based on your code, Incrementing time represented as NBCD format - LooseCaboose - 12-07-2006 Quote:Loose, that's nice. Though that's not really in the nbcd format is it?Depends on the Ada compiler I guess ;-). I could create a record subtype that has both the minutes and seconds inside, but there is no guarantee the compiler would allocate 16 contiguous bits for it (it probably wouldn't). Ada is quite nice for somethings, but utterly horrid for others. I played around with a bit shifty solution, but yours is pretty tough to beat ;-). |