I've read your post about a dozen times and it's helped but I still can't figure out exactly how to do this. Would this be inside a loop or will one command line do it?
: let's try to do this. suppose x = 1010 and you need to invert the
: rightmost bit, or least significant bit (LSB). first you need to
: know what is there, 0 or 1. notice the result when you do bitwise
: AND with 1:
:
: b & 1 = b
:
:
: where b is a binary digit. you get the bit back whenever you AND it
: with 1. so to determine what is the LSB, we need to AND 1 with the
: LSB. the 4 bit binary of decimal 1 is:
:
: 1 = 0001
:
: The result:
:
: x = 1010
: 1 = 0001
: -------- (AND)
: 0000
:
:
: you get 0. so the LSB is 0. you would get a positive number if LSB
: was 1. to innvert this bit, you can now bitwise OR 1 to it:
:
: x = 1010
: 1 = 0001
: -------- (OR)
: 1011
: [/code]:
: to determine the next bit, you can left shift 1 by 1 place. use the
: left shift operator:
:
: b = 1; //consider 4 bit integer
: b <<= 1; //b now contain: 0010 in binary
: [/code]:
: now AND x and b:
:
: x = 1010
: b = 0010
: -------- (AND)
: 0010
:
:
: now you get a positive number, which means the 2nd bit from right is
: 1. to invert it, simply apply
: x = x & ~b;[/code]:
: beware that the result should be checked as unsigned integer.
: consider:
:
: x = 1010
: b = 1000
: -------- (AND)
: 1000
: [/code]:
: in 2's complement system (like todays most computers) the result is
: a negative number, unless you cast it as unsigned integer.
:
: learn more about
:
bit
: wise operators.
:
: hope this helps.
:
~Donotalo()