Hi,

MyByte is byte that could contain binary value between 0000 and 1111

int sum = 0;

                for (int i = 0; i <= 3; i++)

                {

                    if((MyByte & 2^i ) == 1)

                        sum += 2^i;

                }

What will be the value of SUM at the end of this loop ?

tnx,

Recommended Answers

All 4 Replies

Why don't you try running the program yourself? Sheesh.

I know what will be the results. its a general question, is there an equation that can express the value of SUM after the loop in dependence with MyByte value?

You could represent it with a 15-degree polynomial.

Here's a hint: I think it is meant for ^ be an exponentiation operator, not a bitwise xor operator.

If ^ is a exponentiation operator, then the sum will be equal to the MyByte . If the ^ is a XOR operator, then the sum depend on MyByte Since:

[B]ABCD[/B] represents for MyByte binary.
ABCD & (0010 ^ 0000) = 00C0 (it can be 0 or 2)
ABCD & (0010 ^ 0001) = 00CD (it can be 0, 1, 2, 3)
ABCD & (0010 ^ 0010) = 0000
ABCD & (0010 ^ 0011) = 000D (it can be 0 or 1)

As you can see:

  • If MyByte is a even number, then sum must be equal 0.
  • If MyByte is an odd number, then sum must be equal to 1 or 4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.