Convert int to bits

Hello,
I want to convert an int to bits but I didn’t find a cuda function for this purpose. I already have a C++ function. The problem is that I didn’t find the equivalent of function reverse and how can I make this operation x>>=1. If someone could help me?
Thank you

vector<int> convertTobit(int x,int size)
{
    vector<int> ret(size);
    int j=0;

    for(int i=0;i<size;i++)
        ret[i]=0;

    while(x)
    {
        if (x&1)
            ret[j] = 1;
        else
            ret[j] = 0;
        x>>=1;
        j++;
    }

    reverse(ret.begin(),ret.end());
    return ret;
}

What is reverse()? Where, or how, is it defined? “x>>=1” is a standard arithmetic idiom in C and C++ (shorthand for x = x >> 1), and since CUDA is in the C++ family, that should compile just fine.

How about filling your vector in the right order straight away?

for(int i=0; i<size; i++)
        ret = ((x & (1<<i)) != 0);

However if you are really after speed (which is what CUDA is all about), I would try to completely do away with such storage layout fiddling and use the compact representation straight away.

Hello, thank you very much Dear njuffa and tera for your replies. I resolved the problem by using this function. But I don’t know how to minimize the run time.

__global__  void convertTobits(int *ret, int x,int size)
{
	int j = size-1;
	for(int i=0;i<size;i++)
		ret[i] = 0;
	while(x)
	{
	  if(x&1)
	    ret[j] = 1;
	  else
	    ret[j] = 0;
	  x>>=1;
	  j--;
	}
}

tera already showed one way of filling ret in a single pass, rather than in two passes, which should be faster.

With respect to performance, the bigger question is: Why are you trying to transfer the individual bits of an integer into an array of int? You did not show the context of this function, but it seems exceedingly likely that splitting an integer into its constituents bits in this fashion is completely unnecessary, as already alluded to by tera.

Thank you njuffa. I am working on digital watermarking. For this purpose, I need to convert a mark into bits to xor them with other bits and finally embed them in the content to be marked.

You don’t give details, but in the situation you describe one typically extracts bits on the fly to combine them via XOR, without first storing them individually in a temporary array.

For example, I have an image img and a mark m. I want to convert m to bits and xor them with other secret bits and finally, substitute the LSBs of img with the obtained bits.

Let’s assume your image is made up from ‘int’ sized pixels, then based on your description your code might look something like this (presumably, during processing the ‘mark’ in the code below would actually be ‘mark[j]’ for some suitably computed ‘j’, but we don’t know the details of the marking):

#define NBR_LSBS (3)
unsigned int pixel;
const unsigned int mask = ~(~0 << NBR_LSBS);
for (int i = 0; i < NBR_PIXELS; i++) {
   pixel[i] = ((pixel[i] ^ mark) & mask) ^ pixel[i];
}

Parallelizing the loop is like trivial in the case of image processing, as each pixel can be handled separately.

Thank you very much Dear njuffa, I will apply your solution to my situation