What does $(( x^y )) actually do ?
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
-
- Level 1
- Posts: 20
- Joined: Fri Dec 27, 2019 10:16 am
What does $(( x^y )) actually do ?
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.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: What does $(( x^y )) actually do ?
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.
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
-
- Level 16
- Posts: 6054
- Joined: Mon Aug 27, 2012 10:17 pm
Re: What does $(( x^y )) actually do ?
Bitwise XOR.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.
https://www.gnu.org/software/bash/manua ... metic.html
Re: What does $(( x^y )) actually do ?
Code: Select all
Input Input | Output
A B | A OR B
_________________________
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 1
Re: What does $(( x^y )) actually do ?
Irrelevant, this is about XOR.1000 wrote: ⤴Sun Aug 01, 2021 8:24 amTable - how " OR " working from: https://en.wikipedia.org/wiki/OR_gateCode: Select all
Input Input | Output A B | A OR B _________________________ 0 0 | 0 0 1 | 1 1 0 | 1 1 1 | 1
Re: What does $(( x^y )) actually do ?
True. My fault
" bitwise exclusive OR " = XOR
" bitwise exclusive OR " = XOR
-
- Level 1
- Posts: 20
- Joined: Fri Dec 27, 2019 10:16 am
Re: What does $(( x^y )) actually do ?
Thanks for the replies.. I'm obviously not very good at searching !
As they say, if all else fails, RTFM !
As they say, if all else fails, RTFM !