I am performing a bitwise function on 32- and 64-bit integers, and got stuck when writing a method (setValueAt(index, newState)) that sets the bit at index index to 1 if newState is true, or 0 if newState is false. What bitwise function can I use to do the following operation(s) at the same speed as AND, OR, XOR, etc.? I am hesitant to use a detecting method and then an XOR if the bit has to be changed, as this may take too much time; this method has to be called rapidly, possibly hundreds of times in succession depending on the user’s activities.
===setValueAt(3, false)===
0101 0001
? 0000 1000
==============
0101 0001
0101 1001
? 0000 1000
==============
0101 0001
if it helps, the following is a truth table of the desired effect:
|0|1
-+-+-
0|0|0
-+-+-
1|1|0
1 Answer