Home > Blockchain >  Shifting binary number in c
Shifting binary number in c

Time:12-18

For example I want to do: I have 1 it is 00000001 in binary. and when I shift like 1<<3 I'll take 8 it is 00001000. So, I need that I take 00001111 after 1<<3. I wish you understand if something wrong add unclear ask about it please. I want to do shorter this part:

for(int i=1;h>0;h--,i*=2) hr =i;

CodePudding user response:

As I understand, you want

std::uint32_t my_shift(std::uint32_t n, std::uint32_t lshift)
{
    return (n << lshift) | ((1 << lshift) - 1);
//      original shifted | 0b0001111 (lshift 1)
}

CodePudding user response:

You can directly iterate hr:

for(int hr=1; h>0; h--, hr=2*hr 1)
  • Related