Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All,

I am confused with the BitWise Operators.

Can someone please explain the following:

var1 -> 0010 or 0110
var2 -> 0010 or 0100
var3 -> 0010 or 0100
var4 -> 0011 or 0100
((var1 & var2) & (var3 & var4)) -> This tells me I have 4 identical bits set.
Problem is:
var1 -> 0010 or 0100
var2 -> 0010 or 1000
var3 -> 0010 or 0100
var4 -> 0010 or 0100
var5 -> 0001 or 0100

What I am after is anything to tell me that there are 4 identical bits set out of the 5 variables.
Please can anyone help, and thanks in advance.
Posted

The question/problem is not clear to me.
The OR (|) operator outputs 1 if either bits is 1. Otherwise 0.
The And (&) operator outputs 1 if both bits are 1. Otherwise 0.


The following is the truth table for the OR operator. (Bit1 | Bit2 = Output)

Bit1Bit2Output
000
011
101
111



The following is the truth table for the AND operator. (Bit1 & Bit2 = Output)

Bit1Bit2Output
000
010
100
111


 
Share this answer
 
Comments
AspDotNetDev 10-Oct-10 1:04am    
It took me a bit (no pun intended), but I finally understood the question. See my answer. The OP is looking to detect if at least four of the five numbers share at least one position where each bit in that position is set. The question could certainly have been phrased better.
Hopefully this isn't homework, because this is the solution (it's in C#, though you should be able to convert it to C++ fairly easily):
C#
// Test data.
List<uint> nums = new List<uint>();
nums.Add(Convert.ToUInt32("10100010", 2));
nums.Add(Convert.ToUInt32("00101001", 2));
nums.Add(Convert.ToUInt32("10101000", 2));
nums.Add(Convert.ToUInt32("00100001", 2));
nums.Add(Convert.ToUInt32("10000101", 2));


// Target minimum.
int minMatches = 4;


// Variables.
bool success = false;
int count;
int lastBit = (int)Math.Log(Int32.MaxValue, 2);


// Loop through each bit position.
for (int i = 0; i <= lastBit; i++)
{

    // Count the number of bits that are set at the current position.
    count = 0;
    for (int j = 0; j < nums.Count; j++)
    {
        if (((((uint)1) << i) & nums[j]) != 0)
        {
            count++;
        }
    }


    // If the minimum number of matches was found, we're done here.
    if (count >= minMatches)
    {
        success = true;
        break;
    }

}


// Success!
if (success)
{
    string msg = "There were at least {0} numbers that had at least one common set bit.";
    MessageBox.Show(string.Format(msg, minMatches.ToString()));
}
 
Share this answer
 
Assuming aspdotnetdev[^] correctly understood the question, this might be a modern(c++0x required) C++ solution:
C++
// Requires VC2010 C++0x additions

#include <array>
#include <bitset>
#include <algorithm>
#include <iostream>

int main()
{
    typedef std::bitset<8> Val;

    // The test data
    static std::array<Val, 5> vals =
    {
        Val("10100010"),
        Val("00101001"),
        Val("10100010"),
        Val("00100001"),
        Val("10000101")
    };

    // A lambda to sum the bit values at position nPos in vals elements
    auto SumBits = [&](size_t nPos) -> size_t {
        size_t n = 0;
        std::for_each(vals.cbegin(), vals.cend(), [&](const Val& val){n += val.test(nPos);});
        return n; };

    const size_t wanted = 4;
	
    for (auto i = 0; i < Val::_EEN_BITS; ++i) // check each bit position
    {
	auto sum = SumBits(i);
	if (sum >= wanted) // if found report and exit
	{
		std::cout << sum << " bits set at position " << i << std::endl;
		return i;
	}
    }

    std::cout << "No position with "<< wanted << " bits set" << std::endl;
    return 0;
}

cheers,
AR
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900