0

So I have a binary representation of a number as a character array. What I need to do is shift this representation to the right by 11 bits.

For example,

I have a char array which is currently storing this string: 11000000111001 After performing a bitwise shift, I will get 110 with some zeros before it.

I tried using this function but it gave me strange output:

char *shift_right(unsigned char *ar, int size, int shift)
{
int carry = 0;                              // Clear the initial carry bit.
while (shift--) {                           // For each bit to shift ...
    for (int i = size - 1; i >= 0; --i) {   // For each element of the array   from high to low ...
        int next = (ar[i] & 1) ? 0x80 : 0;  // ... if the low bit is set, set the carry bit.
        ar[i] = carry | (ar[i] >> 1);       // Shift the element one bit left and addthe old carry.
        carry = next;                       // Remember the old carry for next time.
    }
}
return ar;
}

Any help on this would be very much appreciated; let me know if I'm not being clear.

2
  • 1
    I have no idea what you think this bitwise-OR with 0x80 is going to do .. we are just operating on characters 1 and 0 here. If you do 0x80 | '1' you will end up with a character that will print as some sort of symbol Commented May 7, 2014 at 5:46
  • Your right, I was trying to use the answer provided here (stackoverflow.com/questions/10367616/…) as a guide but that didn't work, Commented May 7, 2014 at 6:25

3 Answers 3

2

They are just characters...

char *shift_right(unsigned char *ar, int size, int shift)
   {

   memmove(&ar[shift], ar, size-shift);
   memset(ar, '0', shift);

   return(ar);
   };

Or, convert the string to a long-long, shift it, then back to a string:

char *shift_right(char *ar, int size, int shift)
   {
   unsigned long long x;
   char *cp;

   x=strtoull(ar, &cp, 2);  // As suggested by 'Don't You Worry Child'
   x = x >> shift;
   while(cp > ar)
      {
      --cp;
      *cp = (1 & x) ? '1' : '0';
      x = x >> 1;
      }

   return(ar);
   };
Sign up to request clarification or add additional context in comments.

5 Comments

of course, check shift < size and perhaps also shift > 0 before doing this
Why you would need long long to store 14 bits of data...?
The second function worked well. What would I need to add to this if I wanted to store the 11 bits that I shifted out?
@user3610554 Simply save them before calling the shift function.
@user3610554, keep in mind that 'size' is ignored by the second function. Perhaps it could be used to ensure that size is less than, or equal to, 64 bits? (just an afterthought).
0

If you really want to use bitwise shifting, then you can't do it on a string. Simply not Possible!!

You have to convert it to integer (use strtol for that) then do bitwise shifting. After that, convert it back to string (no standard library function for that, use for loop).

Comments

0

I would advise to keep the code simple and readable.

#include <stdio.h>
#include <stdlib.h>

void shift_right (char* dest, const char* source, int shift_n)
{
  uint16_t val = strtoul(source, NULL, 2);
  val >>= shift_n;

  for(uint8_t i=0; i<16; i++)
  {
    if(val & 0x8000) // first item of the string is the MSB
    {
      dest[i] = '1';
    }
    else
    {
      dest[i] = '0';
    }
    val <<= 1;    // keep evaluating the number from MSB and down
  }

  dest[16] = '\0';
}


int main()
{
  const char str [16+1] = "0011000000111001";
  char str_shifted [16+1];

  puts(str);
  shift_right(str_shifted, str, 11);
  puts(str_shifted);

  return 0;
}

2 Comments

This works for my example but it doesn't work if my representation has more bits. How could I do it for a representation of 'x' many bits?
@user3610554 "x" bits doesn't make any sense, nor does anything which isn't a multiple of 8. There must be a specified maximum limit. When you have that, you can easily modify the above to suit any size.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.