开发者

Negate Unsigned Number in Bash

开发者 https://www.devze.com 2022-12-22 17:20 出处:网络
I have a number (hex) and I want the one\'开发者_JAVA技巧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

I have a number (hex) and I want the one'开发者_JAVA技巧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.)


printf "%X" $((255-16#20))

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


Ah, I'm an idiot.

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

Does the trick, or close enough to the trick.

0

精彩评论

暂无评论...
验证码 换一张
取 消