-2
int samples = 8;  

for (unsigned int i = 0; i < (pow(double(2),samples)-1); i++)
{
unsigned int number = i << 1;
}

I am doing the coding in C++ using Opencv library. I want to do bitshift through this and this should be in the range of 0-255 but it is crossing the 255 range and going till 508.

IN Matlab the bitshift operator keeps the range between 0-255. The pattern in matlab is 0,2,4...254,1,3,5...255. But in C++ its going 0,2,4,254,256,258...508. I want the same answer as the matlab. Please suggest me some idea to do that.

hawkeye
  • 349
  • 4
  • 21
  • This sequence doesn't seem to match the [Matlab documentation for bitshift](http://www.mathworks.com.au/help/matlab/ref/bitshift.html). Actually I think what is being described is a [rotate left](http://www.mathworks.com.au/help/fixedpoint/ref/bitrol.html) with 8-bit width – M.M May 15 '14 at 05:37

3 Answers3

4

In C++, i << 1 is equivalent to i * 2, if i is an unsigned integer.

This is nothing to do with "converting 16bit integer to 8bit". One way to do that is to mask off all the bits except the lower 8: (number & 0xFF) or equivalently (number % 256).

If you multiply by 2 and then do this conversion, you will get 0 after 254. But that is still not what you want. Your target output has 1 added to the second half.

So, to get that you could use:

for (int i = 0; i < 256; i++)
    number = (i * 2) % 256 + (i >= 128);
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
M.M
  • 138,810
  • 21
  • 208
  • 365
  • Since Matlab does produce a value of 255, it cannot work modulo 255. – n. m. could be an AI May 15 '14 at 05:30
  • Yeah I saw that afterwards – M.M May 15 '14 at 05:33
  • This doesn't give the desired sequence `0,2,4...254,1,3,5...255`, instead it gives `0, 0, 0, ...` ad infinitum. You need to be _adding_ two somehow rather than multiplying by two. – paxdiablo May 15 '14 at 09:09
  • @paxdiablo, works for me.. http://ideone.com/gBq5Vv . Maybe there was some confusion about variable names – M.M May 15 '14 at 11:00
  • 1
    I can't get at your ideaone code for some reason but I think I understand. You're looping number from 0 to 255 but the variable you construct from it is the expression you gave. That makes sense. I might just add the loop so it makes more sense to me, and matches the variables in the question. Hope you don't mind. – paxdiablo May 15 '14 at 12:37
2

The sequence 0, 2, 4, 6, ..., 252, 254, 1, 3, 5, ..., 253, 255 (which appears to be what you're after based on the sequence you show) can be generated with:

for (int i = 0; i != 257; i = (i == 254) ? 1 : i + 2)
    doSomethingWith (i);

There's probably many other ways to generate that sequence as well, including what's probably a more readable dual-loop version, provided you can keep the body of the loops small:

for (int i = 0; i < 256; i += 2) doSomethingWith (i); // 0, 2, 4, 6, ..., 254
for (int i = 1; i < 256; i += 2) doSomethingWith (i); // 1, 3, 5, 7, ..., 255
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

It looks like Matlab is doing circular 8-bit shift, when abcdefgh << 1 becomes bcdefgha. C++ has no such thing, but you may simulate it by

number = ((i << 1) & 0xff) | (i>> 7);
kocica
  • 6,412
  • 2
  • 14
  • 35
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243