Home > Mobile >  how to change all bits in integer or byte
how to change all bits in integer or byte

Time:11-22

what I'm trying to do is use XOR operand to bits in int so that

what is 0 is than 1¸and

what is 1 is than 0

I've read allot of articles for that and I just seem not to understand it

this is how I'm trying:

    public string text = "password";
    public string keypass = "s0";
    int[] x = new int[text.Length];

    ulong key = 255;
    char encript = ' ';
    x[0] = text[0];
    x[0] ^= x[0] << (int)key;
    encript = (char)x[0];
    keypass = x[0].ToString();
    text = encript.ToString();

it doesn't work

x[0] |= x[0] << (int)key;
x[0] &= x[0] << (int)key;

nothing what I try doesn't seem to work can someone help me here?

CodePudding user response:

To flip all bits in an 8-bit integer, XOR it with 0xFF, i.e. x ^ 0xFF.

Run the below snippet for "proof".

function formatInt(i) {
  return `${i} (0x${i.toString(16).toUpperCase().padStart(2, '0')}) (${i.toString(2).padStart(8, '0')}b)`;
}

for(let i = 0; i < 256; i  ) {
  var l = i ^ 0xFF;
  document.body.appendChild(Object.assign(document.createElement("div"), {innerText: `${formatInt(i)} ^ 0xFF = ${formatInt(l)}`}));
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related