www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - A puzzle (easy one)

reply "Koroskin Denis" <2korden gmail.com> writes:
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! :)
Aug 06 2008
next sibling parent reply BCS <ao pathlink.com> writes:
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); }
Aug 06 2008
parent reply Wyverex <wyverex.cypher gmail.com> writes:
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); }

Aug 06 2008
parent reply Wyverex <wyverex.cypher gmail.com> writes:
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); }


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...
Aug 06 2008
parent reply Frank Benoit <keinfarbton googlemail.com> writes:
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); }


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] ); }
Aug 06 2008
next sibling parent reply Frank Benoit <keinfarbton googlemail.com> writes:
Koroskin Denis schrieb:
 On Thu, 07 Aug 2008 05:59:30 +0400, Frank Benoit 
 <keinfarbton googlemail.com> wrote:
 
 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); }


{ 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] ); }

No-no-no. It should work for integer type of *any* length (byte, short, long, cent and any future type). And there is a *much* simpler solution!

right. template func( T ) { T func = T.max/3; }
Aug 07 2008
parent Frank Benoit <keinfarbton googlemail.com> writes:
Koroskin Denis schrieb:

 right.
 template func( T ) { T func = T.max/3; }

Doesn't work for signed types :P But you are close!

template func( T ) { T func = T.min < 0 ? T.max/3*2+1 : T.max/3; }
Aug 07 2008
prev sibling parent BCS <ao pathlink.com> writes:
Reply to Frank,

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

 

Gaaahhh!!! My eyes <G>
Aug 08 2008
prev sibling next sibling parent "Koroskin Denis" <2korden gmail.com> writes:
On Thu, 07 Aug 2008 05:59:30 +0400, Frank Benoit  
<keinfarbton googlemail.com> wrote:

 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); }


{ 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] ); }

No-no-no. It should work for integer type of *any* length (byte, short, long, cent and any future type). And there is a *much* simpler solution!
Aug 07 2008
prev sibling parent "Koroskin Denis" <2korden gmail.com> writes:
On Thu, 07 Aug 2008 18:11:27 +0400, Frank Benoit  
<keinfarbton googlemail.com> wrote:

 Koroskin Denis schrieb:
 On Thu, 07 Aug 2008 05:59:30 +0400, Frank Benoit  
 <keinfarbton googlemail.com> wrote:

 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); }

overflow...

{ 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] ); }

short, long, cent and any future type). And there is a *much* simpler solution!

right. template func( T ) { T func = T.max/3; }

Doesn't work for signed types :P But you are close!
Aug 07 2008