digitalmars.D.learn - A puzzle (easy one)
- Koroskin Denis (4/4) Aug 06 2008 Write a one-line compile-time function (or template) that returns a
- BCS (6/12) Aug 06 2008 template it(T)
- Wyverex (2/19) Aug 06 2008 Tried something like this, but mine and yours gives integer overflow...
- Wyverex (7/26) Aug 06 2008 template crazy( T )
- Frank Benoit (3/32) Aug 06 2008 template func( T ) { T func = mixin( "0x555555555555555555555555"[0 ..
- Koroskin Denis (5/34) Aug 07 2008 No-no-no. It should work for integer type of *any* length (byte, short, ...
- Frank Benoit (3/40) Aug 07 2008 right.
- Koroskin Denis (4/45) Aug 07 2008 Doesn't work for signed types :P
- Frank Benoit (2/7) Aug 07 2008 template func( T ) { T func = T.min < 0 ? T.max/3*2+1 : T.max/3; }
-
BCS
(3/5)
Aug 08 2008
Gaaahhh!!! My eyes
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
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
BCS wrote:Reply to Koroskin,Tried something like this, but mine and yours gives integer overflow...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
Wyverex wrote:BCS wrote: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...Reply to Koroskin,Tried something like this, but mine and yours gives integer overflow...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
Wyverex schrieb:Wyverex wrote:template func( T ) { T func = mixin( "0x555555555555555555555555"[0 .. T.sizeof*2+2] ); }BCS wrote: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...Reply to Koroskin,Tried something like this, but mine and yours gives integer overflow...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
On Thu, 07 Aug 2008 05:59:30 +0400, Frank Benoit <keinfarbton googlemail.com> wrote:Wyverex schrieb: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!Wyverex wrote:template func( T ) { T func = mixin( "0x555555555555555555555555"[0 .. T.sizeof*2+2] ); }BCS wrote: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...Reply to Koroskin,Tried something like this, but mine and yours gives integer overflow...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 07 2008
Koroskin Denis schrieb:On Thu, 07 Aug 2008 05:59:30 +0400, Frank Benoit <keinfarbton googlemail.com> wrote:right. template func( T ) { T func = T.max/3; }Wyverex schrieb: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!Wyverex wrote:template func( T ) { T func = mixin( "0x555555555555555555555555"[0 .. T.sizeof*2+2] ); }BCS wrote: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...Reply to Koroskin,Tried something like this, but mine and yours gives integer overflow...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 07 2008
On Thu, 07 Aug 2008 18:11:27 +0400, Frank Benoit <keinfarbton googlemail.com> wrote:Koroskin Denis schrieb:Doesn't work for signed types :P But you are close!On Thu, 07 Aug 2008 05:59:30 +0400, Frank Benoit <keinfarbton googlemail.com> wrote:right. template func( T ) { T func = T.max/3; }Wyverex schrieb: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!Wyverex wrote:template func( T ) { T func = mixin( "0x555555555555555555555555"[0 .. T.sizeof*2+2] ); }BCS wrote: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...Reply to Koroskin,Tried something like this, but mine and yours gives integer overflow...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 07 2008
Koroskin Denis schrieb:template func( T ) { T func = T.min < 0 ? T.max/3*2+1 : T.max/3; }right. template func( T ) { T func = T.max/3; }Doesn't work for signed types :P But you are close!
Aug 07 2008
Reply to Frank,func( T ) { T func = mixin( "0x555555555555555555555555"[0 .. T.sizeof*2+2]); }Gaaahhh!!! My eyes <G>
Aug 08 2008