Cycle shift with bitwise operators, help pls

This is a discussion on Cycle shift with bitwise operators, help pls within the C Programming forums, part of the General Programming Boards category; Hello, I'm writing a function which performs a cyclic shift on the bits of an unsigned character with bitwise operators. ...

  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
    21,588
    Post your code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,588
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 02: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, 10:22 AM
  5. Shift operators in C
    By jedi_jinn in forum C Programming
    Replies: 4
    Last Post: 10-23-2006, 02:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21