Share my C++ code , parallel computing one number


  • 0
    J

    in this question it's not so fast compare to others' since not used the previous answer in each iteration
    but quite interesting to compute one single number parallelly

    class Solution {
    public:
        vector<int> countBits(int num) {
            vector<int> result;
            for(int i=0;i<=num;++i){
                result.push_back(countBit(i));
            }
            return result;
        }
        
        int countBit(int num){
            int b = ( ( (num & 0xAAAAAAAA) >> 1) + (num & 0x55555555) );
    	    int c = ( ( (b & 0xCCCCCCCC) >> 2) + (b & 0x33333333) );
    	    int d = ( ( (c & 0xF0F0F0F0) >> 4) + (c & 0x0F0F0F0F) );
    	    int e = ( ( (d & 0xFF00FF00) >> 8) + (d & 0x00FF00FF) );
    	    int f = ( ( (e & 0xFFFF0000) >> 16)+ (e & 0x0000FFFF) );
    	    return f;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.