473,413 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

inputting binary numbers into an int


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
1 1217
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Elliot Marks | last post by:
I want to output this data to text and binary files. The binary file contains the garbage you would expect to see if you try to read it with a text editor, but below that the output appears as...
10
by: J. Campbell | last post by:
OK...I'm in the process of learning C++. In my old (non-portable) programming days, I made use of binary files a lot...not worrying about endian issues. I'm starting to understand why C++ makes...
18
by: Bern | last post by:
how to specifiy a binary number in c++? hex numbers are specified by 0x prefix
6
by: Eric Johnston | last post by:
I want the visitor to enter three numbers on the page and then click a button "generate image" which will I hope cause a generated gif image to be displayed alongside on the page. This involved...
3
by: Tanuki | last post by:
Hi All: I encounter a programming problem recently. I need to read a binary file. I need to translate the binary data into useful information. I have the format at hand, like 1st byte = ID,...
2
by: ASiF | last post by:
Hi i am completely novice at programming, would appreciate some help. The program needs to be able to take a set of up to a 100 numbers (between 1 to 9) "inputted" at the prompt and then assign...
6
by: Kelvin | last post by:
Hi everyone: when we wanna use hex numbers in C, we usually write something like: int hex_num = 0x12F9; but how can I declare a binary number in a similar way by putting some leading words to...
21
by: nicolasg | last post by:
does anyone know a module or something to convert numbers like integer to binary format ? for example I want to convert number 7 to 0111 so I can make some bitwise operations... Thanks
0
Frinavale
by: Frinavale | last post by:
Convert a Hex number into a decimal number and a decimal number to Hex number This is a very simple script that converts decimal numbers into hex values and hex values into decimal numbers. The...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.