Thread: C Read an integer and number of octades

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    13

    C Read an integer and number of octades

    I' m trying to solve a problem:

    The program should read a positive integer and a number for the octades (n).

    The program should show the outcome of the value by rotating the given number by n octades to the right. We have to suppose that the length for integers is 32 bits and that the user enters for the number of octades a value of 1-3. For example, if the user enters 553799974 (bin: 00100001|00000010|01010001|00100110) the program should display: 639697489 (bin: 00100110|00100001|00000010|010 10001).

    I wrote this code for the case of 1 octade:

    Code:
    #include <stdio.h>
    int main()
    {
    
    
        unsigned int num;
        unsigned int temp;
        int n;
    
    
        //printf("Enter number: ");
        //scanf("%d",&num);
        num = 553799974;
        //printf("Enter oct: ");
        //scanf("%d",&n);
        n=1;
        
        if (n == 1){
        
        printf("Num is: %d\nand Temp is: %d\n\n",num, 0xFF);
        
        temp = num & 0xFF; //00000000000000000000000011111111 (under the 32-bit integer).
        
        temp <<= 8;
        
        temp += num >> 8;
        
    }
    
    
        
        printf("Now\n\nNum is: %d\nand Temp is: %d",num, temp);
        
        return 0;
    }
    
    



    Where is my mistake? It returns 2173009 back and not the 639697489

  2. #2
    Registered User
    Join Date
    Dec 2018
    Posts
    13
    OK I managed to solve it myself. Sorry for any inconvenience..

  3. #3
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    138
    [...]

    Quote Originally Posted by akis View Post
    We have to suppose that the length for integers is 32 bits and that the user enters for the number of octades a value of 1-3.
    [...]

    What about including <stdint.h> and using types like uint32_t ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-13-2014, 10:44 PM
  2. How to read Integer from a txt file
    By Oscar Wong in forum C Programming
    Replies: 3
    Last Post: 04-13-2012, 07:33 AM
  3. read a number with scanf() and read char[] with fgets()
    By nutzu2010 in forum C Programming
    Replies: 5
    Last Post: 03-11-2011, 05:05 AM
  4. add one integer with is own number
    By nevrax in forum C Programming
    Replies: 5
    Last Post: 03-30-2007, 02:24 AM
  5. How to read in an integer and display it again
    By axr0284 in forum C++ Programming
    Replies: 7
    Last Post: 12-07-2004, 01:37 PM

Tags for this Thread