What does $(( x^y )) actually do ?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
pete of ebor
Level 1
Level 1
Posts: 20
Joined: Fri Dec 27, 2019 10:16 am

What does $(( x^y )) actually do ?

Post by pete of ebor »

I was just playing about with some simple bash scripts trying to get to grips with arithmetic expansions. I'm aware that to do exponentiation, the operator is a double-asterisk - e.g. $(( 2**4 )) = 16. I tried $(( 2^4 )) to see what would happen. I expected some sort of error, but I got the answer 6. I then wrote a short loop script to do this for x^y where x & Y both ranged from 1-100, and I got a valid answer every time - but I can't work out what it is actually doing. I've searched but to no avail. I'm just curious..
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: What does $(( x^y )) actually do ?

Post by xenopeek »

The manual says ^ is a bitwise exclusive OR operation: https://www.gnu.org/software/bash/manua ... metic.html

Bitwise exclusive OR means to compare each bit of the operands and set the corresponding bit in the result to 1 if the bits differ, or to 0 if they are equal.

Code: Select all

  0010 (2 decimal written as binary)
  0100 (4 decimal written as binary)
^ ----
  0110 = 6 in decimal
Image
Moonstone Man
Level 16
Level 16
Posts: 6054
Joined: Mon Aug 27, 2012 10:17 pm

Re: What does $(( x^y )) actually do ?

Post by Moonstone Man »

pete of ebor wrote: Sun Aug 01, 2021 6:13 am I tried $(( 2^4 )) to see what would happen. I expected some sort of error, but I got the answer 6.
Bitwise XOR.

https://www.gnu.org/software/bash/manua ... metic.html
1000
Level 6
Level 6
Posts: 1040
Joined: Wed Jul 29, 2020 2:14 am

Re: What does $(( x^y )) actually do ?

Post by 1000 »

Code: Select all

Input   Input   |    Output
A 	B 	|    A OR B
_________________________
0 	0 	|    0
0 	1 	|    1
1 	0 	|    1
1 	1 	|    1 
Table - how " OR " working from: https://en.wikipedia.org/wiki/OR_gate
User avatar
xenopeek
Level 25
Level 25
Posts: 29615
Joined: Wed Jul 06, 2011 3:58 am

Re: What does $(( x^y )) actually do ?

Post by xenopeek »

1000 wrote: Sun Aug 01, 2021 8:24 am

Code: Select all

Input   Input   |    Output
A 	B 	|    A OR B
_________________________
0 	0 	|    0
0 	1 	|    1
1 	0 	|    1
1 	1 	|    1 
Table - how " OR " working from: https://en.wikipedia.org/wiki/OR_gate
Irrelevant, this is about XOR.
Image
1000
Level 6
Level 6
Posts: 1040
Joined: Wed Jul 29, 2020 2:14 am

Re: What does $(( x^y )) actually do ?

Post by 1000 »

True. My fault
" bitwise exclusive OR " = XOR
pete of ebor
Level 1
Level 1
Posts: 20
Joined: Fri Dec 27, 2019 10:16 am

Re: What does $(( x^y )) actually do ?

Post by pete of ebor »

Thanks for the replies.. I'm obviously not very good at searching ! :D
As they say, if all else fails, RTFM ! :lol:
Locked

Return to “Scripts & Bash”