Boolean value for byte-byte is about 128

I need to flip a byte value around center 128. So the example outputs of this function include:

In  0     Out  255
In  255   Out  0
In  128   Out  128
In  127   Out  129
In  30    Out  225
In  225   Out  30

      

I'm kidding myself, I'm sure I'll beat myself when I read the answers.

Greetings

+2


a source to share


4 answers


There is no obvious rule that fits your example.



The first two and the last result can be explained 255-n

, which might be a normal "byte mirroring" interpretation. But 128-> 128 should be 256-n

, and 127-> 1, apparently, is inexplicable.

+3


a source


The simple answer, which does not quite match your table 255 - v

.



+1


a source


How about this?

In  0     Out  255
In  255   Out  0
In  128   Out  127
In  127   Out  128
In  30    Out  225
In  225   Out  30

      

It will be 255-n

+1


a source


If you must stay in a byte, use (255-v) + (v==255)?0:1

. Otherwise, use 256-v

. This is a mirror around 128. ~v

(bitwise not) may be faster than 255-v

if you want a mirror around 127.5.

0


a source







All Articles