max value by the number of bits

Hi there,
is there a general template for calculating the max value by the
number of bits. GIL right now uses the following template:
template <int K>
struct unsigned_integral_max_value

2013/2/5 Christian Henning
Hi there,
is there a general template for calculating the max value by the number of bits. GIL right now uses the following template:
template <int K> struct unsigned_integral_max_value
: public mpl::integral_c {}; This code gives me some compiler errors with clang 3.2. for K = 31, for instance. Ideally I would like to take K up to 64.
A small fix gets me up to K = 63
template <int K> struct unsigned_integral_max_value
: public mpl::integral_c {}; Is there a better way in boost?
Try boost::low_bits_mask_t http://tinyurl.com/bcy22xf HTH

Is there a better way in boost?
Try boost::low_bits_mask_t http://tinyurl.com/bcy22xf
Mhmm, I don't see how that works. How do I get the max value from a low_bit_mask_t? It should be 2^n-1 with n being the number the bits. Thanks, Christian

2013/2/5 Christian Henning
Is there a better way in boost?
Try boost::low_bits_mask_t http://tinyurl.com/bcy22xf
Mhmm, I don't see how that works. How do I get the max value from a low_bit_mask_t? It should be 2^n-1 with n being the number the bits.
low_bits_mask_t<n>::sig_bits Not?

Mhmm, I don't see how that works. How do I get the max value from a low_bit_mask_t? It should be 2^n-1 with n being the number the bits.
low_bits_mask_t<n>::sig_bits
Not?
Yes. Thanks! It works but not for n = 64. It gives a wrong value: typedef boost::low_bits_mask_t<64> mask_t; mask_t::least oo = mask_t::sig_bits; Does that work for you? Christian

2013/2/5 Christian Henning
Mhmm, I don't see how that works. How do I get the max value from a low_bit_mask_t? It should be 2^n-1 with n being the number the bits.
low_bits_mask_t<n>::sig_bits
Not?
Yes. Thanks! It works but not for n = 64. It gives a wrong value:
typedef boost::low_bits_mask_t<64> mask_t; mask_t::least oo = mask_t::sig_bits;
Does that work for you?
Yes, I got 18446744073709551615 http://liveworkspace.org/code/3xi9dD$0

Yes. Thanks! It works but not for n = 64. It gives a wrong value:
typedef boost::low_bits_mask_t<64> mask_t; mask_t::least oo = mask_t::sig_bits;
Does that work for you?
Yes, I got 18446744073709551615
Damn, I used MS Calculator to see if all bits are set and I suppose values in Calculator are of a signed type. Of course, you're correct and it does work! Sorry for the confusion, Christian

Le 05/02/13 16:27, Christian Henning a écrit :
Is there a better way in boost?
Try boost::low_bits_mask_t http://tinyurl.com/bcy22xf
Mhmm, I don't see how that works. How do I get the max value from a low_bit_mask_t? It should be 2^n-1 with n being the number the bits.
low_bits_mask_t<Bits>::sig_bits is defined as (~( ~(unsigned_int(0u )) << Bits )) ) which is equivalent to 2^Bits-1 without risk of overflow. Vicente

Le 06/02/13 00:59, Christian Henning a écrit :
low_bits_mask_t<Bits>::sig_bits is defined as (~( ~(unsigned_int(0u )) << Bits )) )
which is equivalent to 2^Bits-1 without risk of overflow.
Vicente, what's unsigned_int?
It is an unsigned type with at least Bits bits. It depends on what do
you want, speed or size.
low_bits_mask_t<Bits>::sig_bits uses the smallest unsigned int and low_bits_mask_t<Bits>::sig_bits_fast uses the fastest one.
"
template

Christian Henning wrote:
Hi there,
is there a general template for calculating the max value by the number of bits. GIL right now uses the following template:
template <int K> struct unsigned_integral_max_value
: public mpl::integral_c {}; This code gives me some compiler errors with clang 3.2. for K = 31, for instance. Ideally I would like to take K up to 64.
A small fix gets me up to K = 63
template <int K> struct unsigned_integral_max_value
: public mpl::integral_c {}; Is there a better way in boost?
boost::int_max_value_t<V> http://www.boost.org/doc/libs/1_53_0/libs/integer/doc/html/boost_integer/int...
Thanks, Christian

boost::int_max_value_t<V>
http://www.boost.org/doc/libs/1_53_0/libs/integer/doc/html/boost_integer/int...
Robert, are you sure I can get the max value of a 15bit value using int_max_value_t? Christian

Christian Henning wrote:
boost::int_max_value_t<V>
http://www.boost.org/doc/libs/1_53_0/libs/integer/doc/html/boost_integer/int...
Robert, are you sure I can get the max value of a 15bit value using int_max_value_t?
whoops - I totally misread your question, I was thinking you wanted the TYPE which would hold a max value corresponding to 15 bits. It turns out that I've been working in this area recently. I would suggest skipping the mpl layer and using BOOST_STATIC_CONSTANT directly. The reason is that mpl includes some operations like next which might not work for 2^64 etc. template <int K> struct unsigned_integral_max_value struct { BOOST_STATIC_CONSTANT( boost::uintmax_t, value = ((boost::uintmax_t)1 << K)-1); }; Robert Ramey

On Tue, Feb 5, 2013 at 4:56 PM, Robert Ramey
Christian Henning wrote:
boost::int_max_value_t<V>
http://www.boost.org/doc/libs/1_53_0/libs/integer/doc/html/boost_integer/int...
Robert, are you sure I can get the max value of a 15bit value using int_max_value_t?
whoops - I totally misread your question, I was thinking you wanted the TYPE which would hold a max value corresponding to 15 bits.
It turns out that I've been working in this area recently. I would suggest skipping the mpl layer and using BOOST_STATIC_CONSTANT directly. The reason is that mpl includes some operations like next which might not work for 2^64 etc.
template <int K> struct unsigned_integral_max_value struct { BOOST_STATIC_CONSTANT( boost::uintmax_t, value = ((boost::uintmax_t)1 << K)-1); };
Isn't this pretty much exactly what Christian was using before? If so, I'd guess it still has the same compile-time-overflow issues. But maybe I'm misremembering the original problem :/ - Jeff

On Feb 5, 2013, at 5:24 PM, "Jeffrey Lee Hellrung, Jr."
On Tue, Feb 5, 2013 at 4:56 PM, Robert Ramey
wrote: Christian Henning wrote: boost::int_max_value_t<V>
http://www.boost.org/doc/libs/1_53_0/libs/integer/doc/html/boost_integer/int...
Robert, are you sure I can get the max value of a 15bit value using int_max_value_t?
whoops - I totally misread your question, I was thinking you wanted the TYPE which would hold a max value corresponding to 15 bits.
It turns out that I've been working in this area recently. I would suggest skipping the mpl layer and using BOOST_STATIC_CONSTANT directly. The reason is that mpl includes some operations like next which might not work for 2^64 etc.
template <int K> struct unsigned_integral_max_value struct { BOOST_STATIC_CONSTANT( boost::uintmax_t, value = ((boost::uintmax_t)1 << K)-1); };
Isn't this pretty much exactly what Christian was using before? If so, I'd guess it still has the same compile-time-overflow issues. But maybe I'm misremembering the original problem :/
- Jeff
No, the original usage was just "-1". -- Marshall Marshall Clow Idio Software mailto:mclow.lists@gmail.com A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait). -- Yu Suzuki
participants (6)
-
Christian Henning
-
Jeffrey Lee Hellrung, Jr.
-
Marshall Clow
-
Robert Ramey
-
TONGARI
-
Vicente J. Botet Escriba