By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,603 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

inputting binary numbers into an int

P: n/a

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

May 25 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Seth King wrote:
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
May 25 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.