19

Unfortunately bc and calc don't support xor.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Costa
  • 552
  • 1
  • 3
  • 10

4 Answers4

41

Like this:

echo $(( 0xA ^ 0xF ))

Or if you want the answer in hex:

printf '0x%X\n' $(( 0xA ^ 0xF ))

On a side note, calc(1) does support xor as a function:

$ calc
base(16)
    0xa
xor(0x22, 0x33)
    0x11
Satō Katsura
  • 13,138
  • 2
  • 31
  • 48
23

With any POSIX shell:

$ printf '%#x\n' "$((0x11 ^ 0x22))"
0x33
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
14

gdb has powerful expression calculator:

gdb -q -ex 'print/x 0xA ^ 0xF' -ex q

A shell function:

calc_gdb() { gdb -q -ex "print/x $*" -ex q;}
calc_gdb 0xA ^ 0xF

$1 = 0x5
lcd047
  • 7,160
  • 1
  • 22
  • 33
Costa
  • 552
  • 1
  • 3
  • 10
7

It is possible to do that in bc:

echo 'xor(10,15)' | bc -l logic.bc

Or in hex:

echo 'obase=16;ibase=16; xor(AA,FF)' | bc -l logic.bc

Using the logic file from here.

Just do wget http://phodd.net/gnu-bc/code/logic.bc to get it.