inputting binary numbers into an int

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am used to programming in embedded C and I want to input a binary number
into an int.

usually I would just use bin as a suffix or b as a prefix

int x = b001 or int x = 001110bin

the hex suffix works 0x
if there is not one is there a standard bin2dec or dec2bin function and
what library it is in
I would think that there would be if there is one in Excel

I just don't know why this is hard to find out

thanks

Seth
 
Seth said:
I am used to programming in embedded C and I want to input a binary number
into an int.

usually I would just use bin as a suffix or b as a prefix

int x = b001 or int x = 001110bin

There's no binary literal in C/C++. It's possible to implement it in C++
using template metaprogramming:

template<unsigned int N>
struct binary
{
enum { digit = N % 10 };
enum { value = digit + (binary<N / 10>::value * 2) };

};

template<>
struct binary<0>
{
enum { value = 0 };

};

int x = binary<001110>::value;

To convert a number to binary at runtime, just mask the upper bit and
shift left (multiply by 2) in a for loop.

Tom
 
Back
Top