Thread: Cycle shift with bitwise operators, help pls

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    69

    Cycle shift with bitwise operators, help pls

    Hello,

    I'm writing a function which performs a cyclic shift on the bits of an unsigned character with bitwise operators.

    For example: my variable is 128, so the bits are 1000 0000, if I cycle 2 then I get 0000 0010.

    It works, my output is fine and everything, but I get an error message: 'stack around the variable number is corrupt'.

    I think it has something to do with the fact that I add an integer to an unsigned char, but how do I correct it? Atoi doesn't work.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post your code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Oh yes, forgot it.

    Code:
    /* AUTH: Kevin Strijbos   DATE: 02/02/2012
       DESC: excercise 12.4 */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void cycle_shift (unsigned char *number, int n);
    
    
    int main (void)
    {
    	unsigned char number;
    	int n;
    
    
    	printf("Enter a number: ");
    	scanf("%d", &number);
    	printf("How many positions do you want to shift: ");
    	scanf("%d", &n);
    
    
    	cycle_shift(&number, n);
    	printf("SHIFTED: %d\n", number);
    
    
    	return 0;
    }
    
    
    /* perfoms a cyclic shift */
    void cycle_shift (unsigned char *number, int n)
    {
    	unsigned char LBIT = 0x80;
    	unsigned char RBIT = 0x01;
    	int counter;
    
    
    	if (n >= 0)
    	{
    		/* left shift */
    		for (counter = 0; counter < n ;counter++)
    		{
    			if (LBIT & *number)
    			{
    				*number <<= 1;
    				*number += 1;
    			}
    			else
    				*number <<= 1;
    		}
    	}
    	else
    	{
    		/* right shift */
    		for (counter = 0; counter < n ;counter++)
    		{
    			if (RBIT & *number)
    			{
    				*number >>= 1;
    				*number += 128;
    			}
    			else
    				*number >>= 1;
    		}
    	}
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps the problem lies here:
    Code:
    scanf("%d", &number);
    You could try using:
    Code:
    unsigned int input;
    scanf("%u", &input);
    number = (unsigned char)input;
    and see if it makes a difference.

    By the way, I think your right shift is broken because counter < n is always false since n < 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Oh indeed, didn't test that part and copied it quickly from my left shift.

    And it works that way indeed, thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shift Operators question
    By kasuka in forum C Programming
    Replies: 1
    Last Post: 11-10-2011, 03:58 PM
  2. Bitwise Shift
    By blurx in forum C Programming
    Replies: 2
    Last Post: 10-12-2008, 09:39 AM
  3. bitwise shift operators in -ve numbers
    By rohit_second in forum C Programming
    Replies: 11
    Last Post: 09-15-2008, 01:18 PM
  4. Bitwise Shift Operations
    By John_L in forum Tech Board
    Replies: 19
    Last Post: 02-25-2008, 11:22 AM
  5. Shift operators in C
    By jedi_jinn in forum C Programming
    Replies: 4
    Last Post: 10-23-2006, 02:52 AM