Boost logo

Boost Users :

From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-07-15 01:15:49


AMDG

Wong, Shin Guey wrote:
>
> Hi all,
>
> This is my first email to this list; please point me out if I done
> anything wrong in this mail like the title format or etc…
>
> I am a newbie to the Boost library. I am really interesting on the
> usage of the Boost preprocessor library. I would like to archive 1 of
> the thing but I am not sure whether it is possible with Boost
> preprocessor library:
>
> Converting the binary number to hexadecimal or decimal value
>
> Eg: BIN2HEX(011) -> 0x3 or BIN2DEC(011) -> 3
>
> BIN2HEX(1111) -> 0xf or BIN2DEC(1111) -> 15
>
> I had read the
> C-Template-Metaprogramming-Concepts-Tools-and-Techniques-from-Boost-and-Beyond-C-in-Depth-Series
> which tell me how to archive that using template. But what I really
> want is to done it with the preprocessor without using any template
> which will not affect the run time but only the compile time.
>
> I know that I can use the loop and the arithmetic function from the
> Boost library but how do I get the binary number characters count, eg.
> COUNT(011) -> 3 or COUNT(0111) -> 4. So that I can use this count with
> loop and the arithmetic function to perform the conversion from binary
> to hexadecimal or decimal.
>

You can't implement BIN2DEC(1111) using Boost.Preprocessor
arithmetic. The only way to do it is:
#define BIN2DEC_0 0
#define BIN2DEC_1 1
#define BIN2DEC_11 2
...
#define BIN2DEC_1111 15
...

There is a way to get BIN2HEX(1 1 1 1) for example:

#include <boost/preprocessor/control/while.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/seq/cat.hpp>
#include <boost/preprocessor/facilities/is_empty.hpp>
#include <boost/preprocessor/tuple/elem.hpp>
#include <boost/preprocessor/logical/not.hpp>
#include <boost/preprocessor/dec.hpp>
#include <boost/preprocessor/arithmetic/mod.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>
#include <boost/preprocessor/tuple/eat.hpp>

#define BINARY_IS_ONE_IMPL_0
#define BINARY_IS_ONE_IMPL_1
#define BINARY_IS_ONE(x)
BOOST_PP_IS_EMPTY(BOOST_PP_CAT(BINARY_IS_ONE_IMPL_, x))

#define BINARY_POP_FRONT_IMPL_0
#define BINARY_POP_FRONT_IMPL_1

#define BINARY_POP_FRONT_IMPL(x) BOOST_PP_CAT(BINARY_POP_FRONT_IMPL_, x)
#define BINARY_CLEAR(x) EMPTY_BINARY

#define BINARY_POP_FRONT(x) BOOST_PP_IF(BINARY_IS_ONE(x), BINARY_CLEAR,
BINARY_POP_FRONT_IMPL)(x)

#define BINARY_IS_EMPTY_IMPL_EMPTY_BINARY
#define BINARY_IS_EMPTY(x)
BOOST_PP_IS_EMPTY(BOOST_PP_CAT(BINARY_IS_EMPTY_IMPL_, x))

#define BINARY_SIZE_IMPL(n, x) (BINARY_POP_FRONT(BOOST_PP_TUPLE_ELEM(2,
0, x)), n)
#define BINARY_SIZE_IMPL_PRED(n, x)
BOOST_PP_NOT(BINARY_IS_ONE(BOOST_PP_TUPLE_ELEM(2, 0, x)))
#define BINARY_SIZE(x) BOOST_PP_TUPLE_ELEM(2, 1,
BOOST_PP_WHILE(BINARY_SIZE_IMPL_PRED, BINARY_SIZE_IMPL, (x, 1)))

#define BINARY_FRONT_IMPL_0 0, ~
#define BINARY_FRONT_IMPL_1 1, ~
#define BINARY_FRONT_IMPL(x, y) x
#define BINARY_FRONT(x) BOOST_PP_CAT(BINARY_FRONT_IMPL,
(BOOST_PP_CAT(BINARY_FRONT_IMPL_, x)))

#define BINARY_TO_HEX_IMPL_0000 0
#define BINARY_TO_HEX_IMPL_0001 1
#define BINARY_TO_HEX_IMPL_0010 2
#define BINARY_TO_HEX_IMPL_0011 3
#define BINARY_TO_HEX_IMPL_0100 4
#define BINARY_TO_HEX_IMPL_0101 5
#define BINARY_TO_HEX_IMPL_0110 6
#define BINARY_TO_HEX_IMPL_0111 7
#define BINARY_TO_HEX_IMPL_1000 8
#define BINARY_TO_HEX_IMPL_1001 9
#define BINARY_TO_HEX_IMPL_1010 A
#define BINARY_TO_HEX_IMPL_1011 B
#define BINARY_TO_HEX_IMPL_1100 C
#define BINARY_TO_HEX_IMPL_1101 D
#define BINARY_TO_HEX_IMPL_1110 E
#define BINARY_TO_HEX_IMPL_1111 F

#define BINARY_GET1(x) (BINARY_FRONT(x))
#define BINARY_GET2(x) (BINARY_FRONT(x))BINARY_GET1(BINARY_POP_FRONT(x))
#define BINARY_GET3(x) (BINARY_FRONT(x))BINARY_GET2(BINARY_POP_FRONT(x))
#define BINARY_GET4(x) (BINARY_FRONT(x))BINARY_GET3(BINARY_POP_FRONT(x))

#define BINARY_EVAL(x) x
#define BINARY_EMPTY
#define BINARY_POP1(x) BINARY_EVAL(BINARY_POP_FRONT BINARY_EMPTY(x))
#define BINARY_POP2(x) BINARY_POP1(BINARY_POP_FRONT(x))
#define BINARY_POP3(x) BINARY_POP2(BINARY_POP_FRONT(x))
#define BINARY_POP4(x) BINARY_POP3(BINARY_POP_FRONT(x))
#define BINARY_TO_HEX_IMPL(n, result)
(BINARY_POP4(BOOST_PP_TUPLE_ELEM(2, 0, result)),
BOOST_PP_CAT(BOOST_PP_TUPLE_ELEM(2, 1, result),
BOOST_PP_CAT(BINARY_TO_HEX_IMPL_,
BOOST_PP_SEQ_CAT(BINARY_GET4(BOOST_PP_TUPLE_ELEM(2, 0, result))))))
#define BINARY_RESULT(n, result) /**/
#define BINARY_TO_HEX_IMPL_SHORT_CIRCUIT(n, result)
BOOST_PP_IF(BINARY_TO_HEX_PRED(n, result), BINARY_TO_HEX_IMPL,
BINARY_RESULT)(n, result)

#define BINARY_TO_HEX_PRED(n, x)
BOOST_PP_NOT(BINARY_IS_EMPTY(BOOST_PP_TUPLE_ELEM(2, 0, x)))
#define BINARY_PAD_TO_4(x) BOOST_PP_REPEAT(BOOST_PP_MOD(BOOST_PP_SUB(4,
BOOST_PP_MOD(BINARY_SIZE(x), 4)), 4), 0 BOOST_PP_TUPLE_EAT(3), ~) x
#define BINARY_TO_HEX(val) BOOST_PP_TUPLE_ELEM(2, 1,
BOOST_PP_WHILE(BINARY_TO_HEX_PRED, BINARY_TO_HEX_IMPL_SHORT_CIRCUIT,
(BINARY_PAD_TO_4(val), 0x)))

BINARY_TO_HEX(1 0 1 0 0 1)

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net