Posts: 788
Threads: 53
Joined: Nov 2002
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
oship me and i will give you lots of guurrls and beeea
Posts: 451
Threads: 16
Joined: Feb 2003
FreeBasic code (tested with Win32 CVS Dec 5, 2006)
Code:
#Define HexPad( Number, Padding ) ( Right( String( Padding, "0" ) & Hex( Number ), Padding ) )
Type sTime Field = 1
As uByte Seconds, Minutes
End Type
Sub Inc( Byval pByte As uByte Ptr )
*pByte += 1
If ( ( *pByte And &H0F ) = &H0A ) Then *pByte += ( &H10 - &H0A )
End Sub
Sub IncTime( Byval pTime As sTime Ptr )
Inc( @pTime->Seconds )
If ( pTime->Seconds = &H60 ) Then
pTime->Seconds = &H00
Inc( @pTime->Minutes )
If ( pTime->Minutes = &H60 ) Then pTime->Minutes = &H00
End If
End Sub
Sub DisplayTime( Byval pTime As sTime Ptr )
Dim As uByte Seconds = ( ( pTime->Seconds Shr 4 ) * 10 ) + ( pTime->Seconds And &H0F )
Dim As uByte Minutes = ( ( pTime->Minutes Shr 4 ) * 10 ) + ( pTime->Minutes And &H0F )
Print Using "##:## 0x"; Minutes; Seconds;
Print HexPad( *cPtr( uShort Ptr, pTime ), 4 )
End Sub
Dim As sTime Clock
DisplayTime( @Clock )
For X As Integer = 1 to 60 * 60
IncTime( @Clock )
DisplayTime( @Clock )
Next
Sleep
End
Life is like a box of chocolates', hrm, WTF, no it isn't, more like, 'life is like a steaming pile of horse crap.'
Posts: 484
Threads: 14
Joined: Apr 2005
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
timestamp += 1
timestamp += Abs((timestamp AND &H000F) = &H000A) * &H0006
timestamp += Abs((timestamp AND &H00F0) = &H0060) * &H00A0
timestamp += Abs((timestamp AND &H0F00) = &H0A00) * &H0600
timestamp += Abs((timestamp AND &HF000) = &H6000) * &HA000
timestamp AND= &HFFFF
Function = timestamp
End Function
' JUST SOME TESTING OUTPUT
Dim ts As Integer
Cls
ts = &H5805
For i As Integer = 0 To (60 * 2)
Locate 1, 1
ts = inc_nbcd_timestamp(ts)
Print "0x" + Hex(ts, 4)
Sleep 150
Next i
EVEN MEN OF STEEL RUST.
Posts: 788
Threads: 53
Joined: Nov 2002
Here's my best shot
Code:
function nbcdTimeIncr ( byval t as integer ) as integer
t1 = t + &ha6a7
m1 = t and &h11110
m2 = t1 and &h11110
nbcdTimeIncr = t1 - ((((not (m1 xor m2)) and &h11110) shr 4)*&hf and &ha6a6)
end function
Add AND &hffff if you care that the upper 16 bits are zero or not.
oship me and i will give you lots of guurrls and beeea
Posts: 484
Threads: 14
Joined: Apr 2005
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
dim as integer t1
t1 = t + &HA6A7
function = t1 - (((((t EQV t1) AND &H11110) shr 4) * &HF) AND &HA6A6)
end function
EVEN MEN OF STEEL RUST.
Posts: 691
Threads: 5
Joined: Apr 2002
Its easy in Ada:
Code:
procedure add is
type time is mod 60;
secs : time := 0;
mins : time := 0;
begin
secs := secs + 1;
if secs = 0 then
mins := mins + 1;
end if;
end add;
esus saves.... Passes to Moses, shoots, he scores!
Posts: 650
Threads: 26
Joined: Aug 2004
i think i misunderstood.
Code:
? mid$(time$,4,2)+right$(time$,2):sleep
quote="whitetiger0990"]whitetiger is.. WHITE POWER!!! [/quote]
Here
Posts: 788
Threads: 53
Joined: Nov 2002
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.
oship me and i will give you lots of guurrls and beeea
Posts: 484
Threads: 14
Joined: Apr 2005
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,
EVEN MEN OF STEEL RUST.
Posts: 691
Threads: 5
Joined: Apr 2002
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 ;-).
esus saves.... Passes to Moses, shoots, he scores!