Solving 2^2/2*2+5-1 and 1 - 5+2*2/2^2 to Get 8

  • Thread starter dE_logics
  • Start date
In summary, the conversation discusses a code that involves calculating mathematical expressions using C. The conversation highlights the use of the bitwise xor operator (^) in C, which is different from an exponent operator and can lead to incorrect results when used in mathematical expressions. The correct way to perform exponentiation in C is to use the pow() function. The conversation also discusses the order of operations in C and how it affects the final result of a mathematical expression.
  • #1
dE_logics
742
0
In the following sources -

Code:
#include<stdio.h>
main()
{
	char a = 2^2/2*2+5-1;
	printf("%d \n", a);
	//Expected - ((((2^2)/2)*2)+5)-1
	//Expected result -- 8
	//Actual result -- 4
	a = 1 - 5+2*2/2^2;
	printf("%d \n", a);
	//Expected - ((((2^2)/2)*2)+5)-1
	//Expected result -- 8
	//Actual result -- -4
}

I expect a result 8 after computation of 2^2/2*2+5-1 or 1 - 5+2*2/2^2 since 1 - 5+2*2/2^2 will mean -
((((2^2)/2)*2)+5)-1 and 2^2/2*2+5-1 will mean -
((((2^2)/2)*2)+5)-1

Which yields 8.
 
Technology news on Phys.org
  • #2
Sooo...is this a bug in C?
 
  • #3
In C, ^ means bitwise xor, not exponentiation.
 
  • #4
So how do we write exponential here?

And what's the actual order then?
 
  • #5
aaaaa...no answers?
 
  • #6
There is no exponent operator in C. If you want to raise a number to a power, use the standard library function pow(). For example, to calculate 22, do something like this:
Code:
#include <math.h>
.
.
.
double x;
x = pow(2.0, 2.0);
 
  • #7
oh...I remember that function though.

So what does exponential do?
 
  • #8
I mean if I've written '^' what will it mean in C?
 
  • #9
Bitwise exclusive 'or' as mXSCNT said:

So take two numbers and the result number has a one bit where one and only one of the bits is on in the two numbers.

25 = 11001
12 = 01100
^. = 10101 = 21
 
  • #10
humm...thanks
 
  • #11
Ok...new issues; in this code -

Code:
#include<stdio.h>
main()
{
	char a = 2/2*2+5-1;
	printf("%d \n", a);
	//Expected - (((2/2)*2)+5)-1
	//Expected result -- 6
	//Actual result -- 6
	a = 1 - 5+2*2/2;
	printf("%d \n", a);
	//Expected -(((2/2)*2)+5)+1
	//Expected result -- 6
	//Actual result -- -2
}

So why does it come - 2 in the second print?
 
  • #12
Order of operations:
a = 1 - 5+2*2/2;
a = 1 - 5+2*1
a = 1 - 5 + 2
a = -4 + 2 = -2
 
  • #13
Code:
	char a = 2 / 2 * 2 [B][COLOR="Red"]+[/COLOR][/B] 5 [B][COLOR="#ff0000"]-[/COLOR][/B] 1;

	a = 1 [B][COLOR="Red"]-[/COLOR][/B] 5 + 2 * 2 / 2;

	//Expected -(((2/2)*2)[B][COLOR="Red"]+[/COLOR][/B]5)[B][COLOR="Red"]+[/COLOR][/B]1

typo?
i don't get it. :(
 
  • #14
Yes, I got it, thanks.
 

Related to Solving 2^2/2*2+5-1 and 1 - 5+2*2/2^2 to Get 8

1. How do you solve 2^2/2*2+5-1 and 1 - 5+2*2/2^2 to get 8?

To solve this problem, we need to follow the order of operations, also known as PEMDAS. First, we solve the exponents, so 2^2 becomes 4. Then, we solve the multiplication and division from left to right, so 4/2 becomes 2 and 2*2 becomes 4. Next, we solve the addition and subtraction from left to right, so 2+5 becomes 7 and 7-1 becomes 6. Finally, we solve the remaining operations, so 1-5 becomes -4 and 2*2 becomes 4. Putting it all together, we get 6 and -4, which equals 2. Therefore, the final answer is 2+4, which equals 6.

2. What is the purpose of using PEMDAS in solving this equation?

PEMDAS, which stands for Parentheses, Exponents, Multiplication and Division (from left to right), and Addition and Subtraction (from left to right), is a set of rules that helps us solve mathematical expressions in a consistent and organized way. By following this order of operations, we can ensure that we get the correct answer every time.

3. Can I solve this equation without following the order of operations?

Yes, technically you can solve this equation without following the order of operations, but it is not recommended. By not following the order of operations, you may end up with a wrong answer. It is important to follow this order to ensure accuracy and consistency in solving mathematical expressions.

4. What are some common mistakes people make when solving this equation?

One common mistake people make is not following the order of operations and solving the equation from left to right without considering the hierarchy of operations. Another mistake is forgetting to apply the multiplication and division before addition and subtraction. It is important to carefully follow the order of operations to avoid these mistakes.

5. Can I use a calculator to solve this equation?

Yes, you can use a calculator to solve this equation. However, it is still important to understand and follow the order of operations when using a calculator. Some calculators may not automatically follow the order of operations, so it is always a good idea to double-check your work by solving the equation manually.

Similar threads

  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
11
Views
858
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
874
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Precalculus Mathematics Homework Help
Replies
10
Views
508
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top