A negative value of a positive number in 2 complement is performed by negating all bits, and then adding 1 to the result. For example, to get -4 out of 4, you would do:
4 = 000...0100 in binary. ~4 = 111...1011. -4 = 111...1100.
Same as vice versa.
So you can do this:
(~((x << 7) - (x << 4))) + 1 .
Not necessarily prettier, but faster if you consider bitwise operations faster than arithmetic operations (especially multiplication) and ignore compiler optimization.
Not that I said that you should do this because you should not. Good to know about it though.
Ivlad source share