A puzzle (easy one)

Frank Benoit keinfarbton at googlemail.com
Wed Aug 6 18:59:30 PDT 2008


Wyverex schrieb:
> Wyverex wrote:
>> BCS wrote:
>>> Reply to Koroskin,
>>>
>>>> Write a one-line compile-time function (or template) that returns a
>>>> number, which can be written in binary as 010101010...010101. Exactly
>>>> the  same code should work for byte, short, int, long, cent etc.
>>>>
>>>> No loops or recursion allowed. Use nothing but brain! :)
>>>>
>>>
>>> template it(T)
>>> {
>>> T it = 
>>> 0x5555_5555_5555_5555_5555_5555_5555_5555_5555_5555_5555_5555_5555_5555_5555_5555 
>>> & (t.max | t.min);
>>> }
>>>
>>>
>> Tried something like this, but mine and yours gives integer overflow...
> 
> 
> template crazy( T )
> {
>   T crazy =  0xAAAA_AAAA_AAAA_AAAA;
> }
> 
> as long as the hex string is less then ulong.max ...  or ucent.max when 
> that gets implemented...

template func( T ) { T func = mixin( "0x555555555555555555555555"[0 .. 
T.sizeof*2+2] ); }


More information about the Digitalmars-d-learn mailing list