inputting binary numbers into an int

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
 
T

Tamas Demjen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top