Skip to content

Binary Invert

Level: 201 · for anyone with a hex editor open

One line: X[i] = ~X[i] flips every bit and takes no operand, and it is the one operation in the dialog that no setting can change — not Treat Data As, not the sign, not the Endian toggle — because inverting a value is the same as inverting each of its bytes.

What the dialog does

The manual writes Binary Invert as X[i] = ~X[i] (Hex Operations). It is Binary Xor with every bit of the operand set, which is why it needs no operand, and it undoes itself.

In Python

Verified output of binary_invert_py.py — regenerated by tools/run_examples.py, never hand-typed.

1. INVERT FLIPS EVERY BIT
------------------------------------------------------------------------
     café         63 61 66 c3 a9
     inverted     9c 9e 99 3c 56
     and again    63 61 66 c3 a9

2. INVERT IS XOR FF
------------------------------------------------------------------------
     all 256 bytes   True

   Xor flips the bits the operand has, and FF has all of them.

3. NO SETTING CAN CHANGE IT
------------------------------------------------------------------------
   Every setting below wrote the same bytes, on every input tried:

     all 65,536 two-byte inputs; Byte and Short, signed and
     unsigned, little and big                                    True
     65,536 four-byte inputs; Byte, Short and Int, both orders   True

   Inverting a value inverts each of its bytes, whichever bytes they
   are and whatever they mean -- so there is nothing for the type, the
   sign or the byte order to decide. Every other operation in the list
   depends on at least one of them.

4. INVERT, THEN ADD 1, IS NEGATE
------------------------------------------------------------------------
     all 256 bytes   True

5. READ AS SIGNED, ~X IS -X - 1
------------------------------------------------------------------------
     every Signed Byte   True
        0 -> ff -> -1
        5 -> fa -> -6
     -128 -> 7f -> 127

   So inverting a Signed Byte never overflows: the most negative value
   becomes the most positive, and the other way round.

No setting can change it

Every other operation in the dialog needs something from the settings: a width to know where a carry stops, a sign to compare or divide, an order to lay out an operand. Invert needs nothing. Flipping every bit of a two-byte value flips every bit of each of its bytes, whichever byte is the low one and whatever the bits are taken to mean. Section 3 checks it without the argument: all 65,536 two-byte inputs under Byte and Short, signed and unsigned, little and big, and 65,536 four-byte inputs as Byte, Short and Int in both orders, and every setting wrote the same bytes every time. The chapter's table reaches the same verdict for all twenty operations, and this is the only row that says no three times.

Invert and negation

Section 4 is two's complement from the other end: Invert and then Add 1 is Negate, for all 256 bytes. Section 5 reads the same fact as signed arithmetic — ~x is −x − 1 for every Signed Byte — which is why an Invert never overflows: 0 becomes −1, and the most negative value, −128, becomes the most positive, 127. Negate has a value it cannot handle; Invert has none.

What the manual does not say

  • Whether Binary Invert is offered for Float and Double. C has no ~ for floating-point values, and nothing on this page says what inverting a float's bytes would mean.

If you are coming from Python or ABAP

Python. Python's ~5 is −6, not FA: its integers have no width, so ~b & 0xFF or b ^ 0xFF is Invert as Unsigned Byte. For a whole buffer, data.translate(bytes(range(255, -1, -1))) maps every byte to its inverse through a 256-byte table in one pass.

ABAP. (Not machine-checked — CI cannot run ABAP.) BIT-NOT inverts a byte-like operand and binds tighter than BIT-AND, BIT-XOR and BIT-OR, and the operand has to be an x or xstring. Since there is no byte order in an operation on bytes, ABAP's BIT-NOT and the dialog's Invert agree on every input without either one having to choose anything.

Try it

  1. Invert a copy of any file and open Tools > Histogram/Entropy on both. The columns should be the same heights, in the opposite order.
  2. Invert one range under every Treat Data As and Endian combination you can, comparing each result with the first. None should differ.
  3. On two copies of a run of bytes, Invert then Add 1 on one and Negate the other, and compare them.
  4. Find FF FF FF FF — the Signed Int −1 — in a copy of a file of your own, Invert it, and read the Inspector.

See also