digitalmars.D.learn - next power of 2
- ref2401 (2/2) Oct 06 2012 is there any Phobos function to calculate the next power of 2 of
- Andrei Alexandrescu (4/6) Oct 06 2012 Use 1U << bsr(x).
- Dmitry Olshansky (9/11) Oct 06 2012 No though it's reinvented in a couple of places I think.
- Dmitry Olshansky (5/14) Oct 06 2012 Yikes, 1<<(bsr(x)+1) or 2 ^^ (bsr(x)+1) ...
- Andrei Alexandrescu (4/19) Oct 06 2012 That's right, apologies for my buggy suggestion and my suggestion of a
- Dmitry Olshansky (4/23) Oct 06 2012 Post of the day :)
- Andrei Alexandrescu (3/12) Oct 06 2012 That's right, apologies for my buggy suggestion.
- bearophile (5/7) Oct 06 2012 See:
is there any Phobos function to calculate the next power of 2 of the specified number?
Oct 06 2012
On 10/6/12 4:48 PM, ref2401 wrote:is there any Phobos function to calculate the next power of 2 of the specified number?Use 1U << bsr(x). http://dlang.org/phobos/core_bitop.html#bsr Andrei
Oct 06 2012
On 07-Oct-12 00:48, ref2401 wrote:is there any Phobos function to calculate the next power of 2 of the specified number?No though it's reinvented in a couple of places I think. Anyway this should work for unsigned numbers: import core.bitop; T nextPow2(T)(T x){ return x == 0 ? 1 : 1^^(bsr(x)+1); } -- Dmitry Olshansky
Oct 06 2012
On 07-Oct-12 00:54, Dmitry Olshansky wrote:On 07-Oct-12 00:48, ref2401 wrote:Yikes, 1<<(bsr(x)+1) or 2 ^^ (bsr(x)+1) ... still haven't got used to 2^^x notation compared to shifts. -- Dmitry Olshanskyis there any Phobos function to calculate the next power of 2 of the specified number?No though it's reinvented in a couple of places I think. Anyway this should work for unsigned numbers: import core.bitop; T nextPow2(T)(T x){ return x == 0 ? 1 : 1^^(bsr(x)+1); }
Oct 06 2012
On 10/6/12 4:57 PM, Dmitry Olshansky wrote:On 07-Oct-12 00:54, Dmitry Olshansky wrote:That's right, apologies for my buggy suggestion and my suggestion of a buggy suggestion :o). AndreiOn 07-Oct-12 00:48, ref2401 wrote:Yikes, 1<<(bsr(x)+1) or 2 ^^ (bsr(x)+1) ... still haven't got used to 2^^x notation compared to shifts.is there any Phobos function to calculate the next power of 2 of the specified number?No though it's reinvented in a couple of places I think. Anyway this should work for unsigned numbers: import core.bitop; T nextPow2(T)(T x){ return x == 0 ? 1 : 1^^(bsr(x)+1); }
Oct 06 2012
On 07-Oct-12 00:58, Andrei Alexandrescu wrote:On 10/6/12 4:57 PM, Dmitry Olshansky wrote:Post of the day :) -- Dmitry OlshanskyOn 07-Oct-12 00:54, Dmitry Olshansky wrote:That's right, apologies for my buggy suggestion and my suggestion of a buggy suggestion :o).On 07-Oct-12 00:48, ref2401 wrote:Yikes, 1<<(bsr(x)+1) or 2 ^^ (bsr(x)+1) ... still haven't got used to 2^^x notation compared to shifts.is there any Phobos function to calculate the next power of 2 of the specified number?No though it's reinvented in a couple of places I think. Anyway this should work for unsigned numbers: import core.bitop; T nextPow2(T)(T x){ return x == 0 ? 1 : 1^^(bsr(x)+1); }
Oct 06 2012
On 10/6/12 4:54 PM, Dmitry Olshansky wrote:On 07-Oct-12 00:48, ref2401 wrote:That's right, apologies for my buggy suggestion. Andreiis there any Phobos function to calculate the next power of 2 of the specified number?No though it's reinvented in a couple of places I think. Anyway this should work for unsigned numbers: import core.bitop; T nextPow2(T)(T x){ return x == 0 ? 1 : 1^^(bsr(x)+1); }
Oct 06 2012
On Saturday, 6 October 2012 at 21:00:57 UTC, ref2401 wrote:is there any Phobos function to calculate the next power of 2 of the specified number?See: http://d.puremagic.com/issues/show_bug.cgi?id=6343 Bye, bearophile
Oct 06 2012