Thread: Some problems with bitwise operators and char

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    2

    Some problems with bitwise operators and char

    Hi guys this is my first post in here and I hope to be an active user

    I got some problems with this code.
    It is supposed to rotate of n_bit the input char and print the bit rappresentation

    Now here is the not working code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    char rotate_c(char c, short n_bit);
    void arr_bit (short arr[], char c);
    
    
    int main() {
        char c;
        char c_rot;
        short n, arr[8], arr_rot[8];
    
    
       printf("Insert char\n-> ");
       c = getchar();
       printf("%c = ",c);
       arr_bit(arr,c);
       for (n = 0; n < 8; n++)
          printf("%d",arr[n]);
    
       printf("\n [(-7,-1)(1-7)]\n-> ");
       scanf("%d", &n);
       c_rot = rotate_c(c,n);
    
       printf("Your char: %c\n", c);
       printf("modified: %c\n", c_rot);
       arr_bit(arr_rot,c_rot);
    
       for (n = 0; n < 8; n++)
          printf("%d",arr_rot[n]);
    }
    
    
    char rotate_c(char ch, short n_bit) {
        char car = ch;
        char temp;
    
    
        if (n_bit < 0) {
            n_bit = abs(n_bit);
            temp = car;
            car = car<<n_bit;
            temp = temp>>(8-n_bit);
            car = car|temp;
        }
    
    
        if (n_bit > 0) {
            temp = car;
            car = car>>n_bit;
            temp = temp<<(8-n_bit);
            car = car|temp;
        }
    
    
        return car;
    }
    
    
    void arr_bit (short arr[], char c) {
        short i = 7;
    
    
        while (i >= 0) {
            if (c != 0) {
                arr[i--] = c&1;
                c = c>>1;
            }
            else {
                arr[i--] = 0;
            }
        }
    
    
    }
    In this case, the input variable is modified by the rotate_c function and the bit rappresentation of the character is 1111 1111
    always.
    Now if I change the variable n from short to int it is fully working!
    And I just can't figure out why!!
    Anyone can help me understanding why?
    Sorry for the long post and see ya soon
    Last edited by dpntn; 07-18-2016 at 04:00 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    A quick lesson on how to use a debugger.
    Code:
    $ gcc -g bar.c
    $ gdb -q ./a.out 
    Reading symbols from /home/sc/Documents/a.out...done.
    (gdb) break rotate_c 
    Breakpoint 1 at 0x400760: file bar.c, line 38.
    (gdb) run
    Starting program: /home/sc/Documents/a.out 
    Insert char
    -> @
    @ = 01000000
     [(-7,-1)(1-7)]
    -> -1
    
    Breakpoint 1, rotate_c (ch=64 '@', n_bit=-1) at bar.c:38
    38        char car = ch;
    (gdb) n
    42        if (n_bit < 0) {
    (gdb) n
    43          n_bit = abs(n_bit);
    (gdb) n
    44          temp = car;
    (gdb) print n_bit
    $1 = 1
    (gdb) n
    45          car = car<<n_bit;
    (gdb) n
    46          temp = temp>>(8-n_bit);
    (gdb) print car
    $2 = -128 '\200'
    (gdb) print/x car
    $3 = 0x80
    (gdb) n
    47          car = car|temp;
    (gdb) print temp
    $4 = 0 '\000'
    (gdb) n
    51        if (n_bit > 0) {
    (gdb) print car
    $5 = -128 '\200'
    (gdb) print n_bit
    $6 = 1
    So, n_bit is suddenly 1 and you're about to enter the other if statement in rotate_c.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2016
    Posts
    2
    Hi and thanks for the answer!
    You're right! But after the run and the call to the function rotate_c, even if I correct the if-else statement, the input character is modified using the variable n as short and I can't figure out why! If I compile and run the same code but with the variable n as int it seems to be fully working!
    Thank you and sorry for the stupid questions if they are!

    EDIT
    Found the solution. It is in the scanf process waiting for an int ("%d"). I changed that to ("%hd") and it's working now even with n declared as a short. Thank you for the interest!
    Last edited by dpntn; 07-18-2016 at 07:06 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise Operators
    By (^Burt^) in forum C Programming
    Replies: 4
    Last Post: 10-24-2013, 01:20 AM
  2. Bitwise operators help ~
    By Siaw Ys in forum C Programming
    Replies: 7
    Last Post: 12-11-2011, 02:02 AM
  3. Bitwise operators
    By shethecoder in forum C Programming
    Replies: 6
    Last Post: 05-30-2011, 10:33 PM
  4. Bitwise operators
    By Unregistered in forum C Programming
    Replies: 22
    Last Post: 03-24-2004, 11:03 AM
  5. Bitwise operators. What's this????????
    By money? in forum C Programming
    Replies: 20
    Last Post: 06-17-2003, 06:03 PM

Tags for this Thread