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
SUMMARY

The discussion focuses on the confusion surrounding the use of the caret (^) operator in C programming, which represents bitwise XOR rather than exponentiation. Participants clarify that to perform exponentiation in C, the pow() function from the math.h library should be used. The expected results of certain arithmetic expressions, such as 2^2/2*2+5-1 and 1 - 5+2*2/2^2, do not yield 8 due to the incorrect use of the XOR operator. The correct order of operations is also discussed, highlighting how it affects the final output.

PREREQUISITES
  • Understanding of C programming syntax
  • Familiarity with arithmetic operations and operator precedence
  • Knowledge of the math.h library and its functions
  • Basic concepts of bitwise operations in C
NEXT STEPS
  • Learn how to use the pow() function for exponentiation in C
  • Study operator precedence and associativity in C programming
  • Explore bitwise operations and their applications in C
  • Practice debugging arithmetic expressions in C to understand output discrepancies
USEFUL FOR

C programmers, software developers, and students learning C who seek to clarify arithmetic operations and operator usage in their code.

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
4K
  • · Replies 1 ·
Replies
1
Views
1K
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
2K