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

  • Thread starter Thread starter dE_logics
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the evaluation of mathematical expressions in C programming, specifically focusing on the operations involving exponentiation and the order of operations. Participants explore the discrepancies between expected and actual results when using certain operators.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents code snippets expecting the result of certain expressions to equal 8, but receives different actual results, leading to questions about operator behavior.
  • Another participant notes that in C, the '^' operator represents bitwise XOR rather than exponentiation.
  • A participant inquires about how to perform exponentiation in C, leading to the suggestion to use the `pow()` function from the standard library.
  • Further discussion reveals confusion about the order of operations in a different code snippet, where the expected and actual results differ significantly.
  • Participants analyze the order of operations in the expression `1 - 5 + 2 * 2 / 2`, breaking it down step by step to clarify the outcome.
  • One participant expresses confusion about a potential typo in the expected results presented in the code snippets.

Areas of Agreement / Disagreement

Participants generally agree on the behavior of the '^' operator in C and the need for the `pow()` function for exponentiation. However, there is some confusion regarding the expected results of the expressions and the order of operations, indicating unresolved issues.

Contextual Notes

There are limitations in the discussion regarding the assumptions made about operator precedence and the expected outcomes of the expressions, which are not fully resolved.

dE_logics
Messages
742
Reaction score
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
Sooo...is this a bug in C?
 
In C, ^ means bitwise xor, not exponentiation.
 
So how do we write exponential here?

And what's the actual order then?
 
aaaaa...no answers?
 
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);
 
oh...I remember that function though.

So what does exponential do?
 
I mean if I've written '^' what will it mean in C?
 
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]+[/B] 5 [B]-[/B] 1;

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

	//Expected -(((2/2)*2)[B]+[/B]5)[B]+[/B]1

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

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
7
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
Replies
1
Views
1K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K