Public Group

# binary math

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

## Recommended Posts

i would like to do some binary math... this is what i want to do... have 2 variables with 20 binary numbers each... i want to be able to set these binary numbers one and one shift them either to the left or right (shift operators << >> i guess) perform a binary AND operation between them and then get how many binary 1's the result has... how would i do this using c++?

##### Share on other sites
You can isolate specific binary digits by doing a bitwise AND, or using the shift operators.

For example:

01001110

Let's say you wanted to extract the 2nd digit, from the left. You could do a bitwise AND:

01001110
& 01000000
-----------
01000000

Or you could shift it out.
01001110 << 1 >> 6

In C++, the bitwise operators are:
<< left shift
>> right shift
& bitwise AND
| bitwise OR
^ bitwise XOR
~ one's complement

##### Share on other sites
As far as finding out how many '1's a binary representation of a number has, I'll refer you to this standard "Algorithmic Design" problem:
std::string strBinary;
if (n == 0)
strBinary = "0";
else
{
while (n > 0)
{
if (n % 2 == 1)
strBinary.insert(0, "1");
else
strBinary.insert(0, "0");
n /= 2;
}
}
It'll give you the binary representation of an integer 'n'. I'll leave it to you to modify it to count the ones.

I'm not sure what
Quote:
 i want to be able to set these binary numbers one and one
means. Sc4Freak's example shows you how to shift bits, although you'll want to change
01001110 << 1 >> 6
to
01001110 << 1 >> 7
(01100000 << 1 >> 6 = 00000011, yet 01100000 << 1 >> 7 = 00000001)

To set a binary number, you'll need a bit mask, most likely. In any event, knowing how to use them will be helpful if you want to do anything meaningful.

Best of luck!
-jouley

##### Share on other sites
thx...

is there anyway to use use and crate variables directly in binary form as in assembler?

like

int number256 = x11111111 (which would set the first 8 binary numbers of 32 in the int, the rest would be 0)

and vice versa?

##### Share on other sites
i was just wondering if its possible to do AND, OR, XOR and shift operations on bool arrays? that would solve all my problems...

somethin like...

bool binary1[20];
bool binary2[20];

bool binary3[20] = binary1 || binary2;

EDIT:: on second thought i guess i could do my own functions for that

##### Share on other sites
Quote:
 Original post by Dragon_Strikethx...is there anyway to use use and crate variables directly in binary form as in assembler?likeint number256 = x11111111 (which would set the first 8 binary numbers of 32 in the int, the rest would be 0)and vice versa?

You can most definitely set variables using hex just as easily as you would decimal. However, 0x100000000 is 256, not x11111111.
Quote:
 i was just wondering if its possible to do AND, OR, XOR, operations on bool arrays? that would solve all my problems...somethin like...bool binary1[20];bool binary2[20];bool binary3[20] = binary1 || binary2;
It's possible, but you'll have to index things as far as I know. That is:
bool binary1[20];
bool binary2[20];

// to OR the arrays together:
bool binary3[20];
for (int i = 0; i < 20; ++i)
binary3 = binary1 || binary2;

-jouley

##### Share on other sites
Moved to For Beginners.

##### Share on other sites
A simpler way to count the ones without messing with strings would be the following.

int getNumOnes(int i)
{
int res = 0;
for(int t=1;t<=i;t*=2)
if((t&i) > 0) ++res;

return res;
}

1. 1
2. 2
Rutin
29
3. 3
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• ### Forum Statistics

• Total Topics
633648
• Total Posts
3013111
×