# Js implementation of specific bit inverse principle and examples

- 2020-03-30 03:28:24
- OfStack

When I went to huawei for an interview, I didn't prepare well. The interview process did not ask clear also did not check, the results of the past let on the machine to do questions, really a bit unprepared. The author is good at front-end Java Web engineer ah, the basic underlying programming knowledge has long been unfamiliar. Machine test paper encountered the problem of this bit operation, is supposed to be very simple, the principle of the author is also very clear, but because it is really many years have not done the bit operation, Java bit operation is not operation, so the result is really not decent...

Computer test time is an hour, the language can choose C or Java there is a scripting language, the author spent nearly three hours, just barely use Java to work out the problem, ashamed... Come back with JS to re-implement a simple version, today tidy up posted.

The question is: loop in two Numbers for each group hex and n(0< = n< 31), hex is a hexadecimal number. What we need to do is invert hex to the NTH bit and then output the corresponding result in hexadecimal form.

The author of more than two hours of process will not be repeated, here give js implementation, very simple bit operation basic knowledge. The principle is to move 1 bit bit to the left by n, and then with the original number or:

```
function bitOper(hex, n){
var num = parseInt(hex);
num ^= (1<<n);
return num.toString(16);
}
console.log(bitOper(0x1234, 3)); //123c
```