Thread: Please, tell me why this code does not work

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    13

    Please, tell me why this code does not work

    Please help. I am new to C. I am trying to display the character 'W' as it is written in binary using a bitmask. I am aware there are alternate ways to do this, but I want to know why this did not work. If you read the comments, I know the problem is somewhere in the 'if' statement, because the code works when I delete the if statement. The compilers error message says: ...17 (line of the if statement): "invalid operands to binary & (having 'int' and 'char *)". Yet letter & array are both char variables. So I do not understand:
    Code:
    #include <stdio.h>
    
    
    
    
    int main(void){
    
    
    int i = 0;
    char letter = 'W';
    char array[] = "0b00000000";
    char *pointer = array;
    
    
    // so first, i want the pointer + 2 to change the 0 (in array) to a 1. But I want this to be an increment, not a pointer switch.
    
    
        for (i = 2; i < 10; i++){
        *(pointer + i) = '1';
    
    
        // Now I have the bitmask. So I want to apply the bitmask to W using a boolean operator, and print either a 1 or 0 according
        // to which bit value is contained in 'W'. I also want to undo the '1' in the bitmask at the end of the loop, setting it to 0, then moving
        // on to the next bit in the bitmask; changing that to a 1, and repeating the process until all 8 bits have been checked.
    
    
        if (letter & array){
            printf("1");
        }
        // So at this point array = 0b10000000. The printf statement confirms that. 
        //
        // I am aware this code is not complete. I'd planned to add the statement:
        // if (~(letter & array)){
        // printf("0");
        // }
    
    
        printf("%s", array);
    
        // If I do not include the 'if' statement, the printf function runs and displays my bitmasks, incremented from 0b10000000 to 0b01000000
        //and so on. But, when I add the if statement, it no longer works. 
    
        *(pointer + i) = '0';
        }
    
    
    return 0;
    }
    Last edited by Ian Rust; 05-30-2015 at 11:43 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that in the expression (letter & array), array is converted to a pointer to its first element, then bitwise and is computed. This does not result in the bitmask that you were expecting.

    Rather, what you should do is to have something like:
    Code:
    unsigned int mask = 1 << (CHAR_BIT - 1);
    Then:
    Code:
    if (letter & mask)
    This way, you can then write:
    Code:
    mask >>= 1;
    At the end of the loop, looping for CHAR_BIT number of iterations.
    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
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    ^_^

    Code:
    char array[] = "0b00000000"; //wrong
    
    uint8_t/unsigned char array = 0b00000000; //correct
    This link pretty much sums up everything about bitmasks How do you set, clear and toggle a single bit in C/C++? - Stack Overflow .

  4. #4
    Registered User
    Join Date
    May 2015
    Posts
    13
    Ok, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The code does not work
    By Satya in forum C Programming
    Replies: 7
    Last Post: 04-10-2015, 12:50 AM
  2. How this c++ code work
    By Username changed in forum C++ Programming
    Replies: 5
    Last Post: 12-28-2013, 10:28 PM
  3. Cant seem to get this code to work.....
    By Alex Coven in forum C Programming
    Replies: 2
    Last Post: 09-10-2013, 06:57 PM
  4. Just Can't Get This Code to Work
    By WMH in forum C Programming
    Replies: 20
    Last Post: 09-19-2012, 04:25 PM
  5. My Code Does Not Work? Why?
    By mintsmike in forum C Programming
    Replies: 8
    Last Post: 03-24-2009, 02:03 PM