Thread: circular shift

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    7

    Unhappy circular shift

    ok, im trying to write a function that does a "circular shift" i.e: left shifts "a" by "n" positions, where high order bits are introduced as low order bits.

    eg:

    10000001 circular shift 1 yeild = 00000011
    01101011 circular shift 3 yields = 01011011

    heres my code so far...it shifts but does set do the circular thing:

    #include <stdio.h>

    int circular_shift(int a, int n);

    int main(void)
    {
    int num, yeilds;
    char numstring[81];

    printf("Enter a number:\n");
    gets(numstring);
    num = atoi(numstring);

    printf("Enter the number of shift yields:\n");
    gets(numstring);
    yeilds = atoi(numstring);

    circular_shift(num, yeilds);

    getch();
    return 0;
    }
    int circular_shift(int a, int n)
    {
    int circular, result, i;

    result = a << n;

    for(i =1; i<= 8; i++)
    {
    printf("%d", (result & 0x0080) !=0);

    result <<=1;
    }
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    36
    >for(i =1; i<= 8; i++)
    >{
    >printf("%d", (result & 0x0080) !=0);
    in tc 2.0 sizeof(int) is 2, in gcc it is 4,so replace it with:

    for(i =1; i<= sizeof(result)*8; i++)
    {
    printf("%d", (result & (1<<(sizeof(result)*8-1))) !=0);

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    7

    Thumbs up

    thanks for that..works perfectly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. still problems with ceasar shift
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 09-14-2008, 01:49 AM
  2. Ceasar Shift program
    By trevordunstan in forum C Programming
    Replies: 11
    Last Post: 09-11-2008, 09:40 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. Caeser Cipher
    By L_U_K_E in forum C++ Programming
    Replies: 35
    Last Post: 06-30-2006, 07:15 AM
  5. Simulating Shift Key
    By PrimeTime00 in forum Windows Programming
    Replies: 7
    Last Post: 10-15-2004, 05:53 AM