0

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,

3
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by invisal
0

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?

0

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.

0

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
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.