What are the general uses of bit operators?

What are the general uses of the

bit operator? For example, to solve a problem, it is very simple to use the bit operator.

Mar.29,2021

such as color value conversion, hexadecimal to rgb


function hexToRgb(hex){
    var hex = hex.replace("-sharp","0x"),
        r = hex >> 16,
        g = hex >> 8 & 0xff,
        b = hex & 0xff;
    return "rgb("+r+","+g+","+b+")";
}

such as recording activity status and growth value. Use bit operations to give a variable multiple states
you can see this example; use bit operations to give a variable multiple states


A non-empty array with only one element appearing odd times and the rest even times. Find out which element:

    int singleNumber(vector<int>& nums) {
      return std::accumulate(nums.begin(), nums.end(), 0, [](int res, int n) { 
          res ^= n;
          return res;
        });
    }

A non-empty array with only one element appearing once and the rest three times. Find out which element:

    int singleNumber(vector<int>& nums) {
      int twos = 0;
      return std::accumulate(nums.begin(), nums.end(), 0, [&twos] (int ones, int n) { 
          ones ^= n & ~twos; 
          twos ^= n & ~ones; 
          return ones;
        });
    }

A non-empty array with only two elements appearing once and the rest twice. Find out which two elements:

     vector<int> singleNumber(vector<int>& nums) {
               
        int xr=nums[0];
        for(int i=1;i<nums.size();iPP)
            xr=xr^nums[i];
        
        int pos=xr&~(xr-1);

        int x=0,y=0;
        for(int i=0;i<nums.size();iPP)
            if((pos)&nums[i])
                x=x^nums[i];
            else
                y=y^nums[i];
        
        vector <int> res;
        res.push_back(x);
        res.push_back(y);
        return res;
    }

Carmack algorithm:

//
// x
//
float InvSqrt (float x)
{
    float xhalf = 0.5f*x;
    int i = *(int*)&x;
    i = 0x5f3759df - (i >> 1); // 
    x = *(float*)&i;
    x = x*(1.5f - xhalf*x*x); // 
    return x;
}

    :
    $exchange = function (){
        $a = 19;
        $b = 20;

        $b = $a ^ $b;
        $a = $b ^ $a;
        $b = $a ^ $b;

        echo $a.'-'.$b.PHP_EOL;
    };
    $exchange();
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e48202-44417.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e48202-44417.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?