views:

91

answers:

2

I have a number (hex) and I want the one's complement of it. For example if X = 20 I want bash to perform a negation and return Y = ~X = DF. It doesn't have to be in bash, but it should use a common command line tool that I can wrap into a script. Also note that the numbers should all be unsigned and I'd prefer they don't overflow the bits available (e.g., 20 would only be 8 bits, so the output should be the 8-bit one's complement of that).

I've tried various things using bash and bc, but haven't found the right combo. Any ideas?

(If anyone cares, the goal is to set the IRQ affinity to a specific CPU and then set the other IRQs to all other processors.)

A: 

Ah, I'm an idiot.

printf "%x\n" $(echo $((~ 16#$INPUT)))

Does the trick, or close enough to the trick.

mjschultz
The `$(echo) isn't necessary: `printf "%x\n" $((~ 16#$INPUT))`
Dennis Williamson
+1  A: 
printf "%X" $((255-16#20))

The 16# tells bash that the number is in base 16.

Ignacio Vazquez-Abrams