# binary numbers

This topic is 3329 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I need to represent a strip of data of which each element can be either true or false, and i need access to each element to edit it. The logical way to represent this seemed to me to be binary numbers. So my question is, if I define a binary number like so: #define BN 0000 0101 How can I, for example, check if the 3rd number is a zero and if it is change it to a 1? I would consider converting the binary to a string but would it be better then to just use a string in the first place (also considering speed of the program)? Thanks A_B

##### Share on other sites
Unfortunately, C++ provides no binary literals. But if the numbers fit into 10 bits, you can use some template metaprogramming:

#include <iostream>template<unsigned n>struct bit;template<>struct bit<0>{    enum { value = 0 };};template<>struct bit<1>{    enum { value = 1 };};template<unsigned n>struct binary{    enum { value = bit<n%10>::value + 2*binary<n/10>::value };};template<>struct binary<0>{    enum { value = 0 };};int main(){    std::cout << binary<  0>::value << std::endl;    std::cout << binary<  1>::value << std::endl;    std::cout << binary< 10>::value << std::endl;    std::cout << binary< 11>::value << std::endl;    std::cout << binary<100>::value << std::endl;    std::cout << binary<101>::value << std::endl;    std::cout << binary<110>::value << std::endl;    std::cout << binary<111>::value << std::endl;}

Quote:
 Original post by A_BHow can I, for example, check if the 3rd number is a zero and if it is change it to a 1?

If it is 0, you want to set it to 1, and if it is 1, you want to leave it at 1? Then there's no need for the if -- just set it to 1.
int number = binary<1011>::value;    number|= binary< 100>::value;

##### Share on other sites
Quote:
 How can I, for example, check if the 3rd number is a zero and if it is change it to a 1?

value |= 4; // 4 == 100b

Quote:
 I would consider converting the binary to a string but would it be better then to just use a string in the first place (also considering speed of the program)?

std::vector<bool> is specialized for this purpose. It stores the booleans as bits rather than bools, to save some memory. It therefore behaves a little different than a normal vector, but that's likely not a problem for your purposes.

##### Share on other sites
Thanks all,

It seems quit strange to me that a programming language does not allow working with binary numbers, since that is what the computer actually does. You replies helped me a lot, thanks.

A_B

##### Share on other sites
Quote:
 Original post by A_BIt seems quit strange to me that a programming language does not allow working with binary numbers, since that is what the computer actually does.

Actually it doesn't. Well, not in the way you want. A 32-bit computer will manipulate one 32-bit number at a time but cannot access individual bits. Some (8051 i believe) have bit-wise access to parts of memory but that is not really needed nowadays. To get individual bits in C/C++ use bit masking like what Captain P demonstrated.

##### Share on other sites
Quote:
 Original post by A_BIt seems quit strange to me that a programming language does not allow working with binary numbers, since that is what the computer actually does. You replies helped me a lot, thanks.

It does allow that, by using something like the following, to check if the third bit from the right is 0 and if so change it to a 1:
if((BN & (1 << 3)) == 0) BN |= (1 << 3);

The computer doesn't really work with bits in that way though, almost everything is done on 32-bit numbers at a time in a 32-bit program. The actual instructions in the CPU work on 32 bits at a time, and it's often at least as fast to change the whole 32 bit number rather than just one of it's bits, unless you have a very specific situation where using less memory is beneficial.

##### Share on other sites
If you don't need the values at compile time, you can also parse strings at runtime:
unsigned binary(char const* s){    int x = 0, c;    while (c = *s++) x += x + (c - '0');    return x;}int main(){    std::cout << binary("00010010") << std::endl;}

##### Share on other sites
I love you, Weetbix :p
that's exactly what I needed.

##### Share on other sites
Quote:
 Original post by A_BThanks all,It seems quit strange to me that a programming language does not allow working with binary numbers, since that is what the computer actually does. You replies helped me a lot, thanks.A_B

Though consider that actual binary representations are generally not relevant to a programming language itself; a compiler for C++ could as well be written for a machine that has more than 2 states per transistor (the reason why binary states have been chosen is that they can be stored in the most stable way, the more states you add per transistor, the more error prone storage is thanks to things like fluctuations in electricity; with binaries, there is a maximum electricity below which the state of the transistor is interpreted as false, or zero, then there's a dead zone for which digits are interpreted as corrupt, and beyond some minimum electricity it is interpreted as true; all that is of course very simplified).

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
631775
• Total Posts
3002278
×