Thread: What does this statement do ?

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    33

    What does this statement do ?

    The program main call a func that just counts the binary 1 in an 8 bit number. Please see line in RED below.

    I do not know how result is incremented based on the highlighted line. I looked through my C programming book and could not find anything that describes the statement. It does work however.

    Code:
    int i=1, myInt;
    
    int main(int argc, char *argv[]) {
    
    i=37; //"00100101" in binary, contains three bits that are set to "1".
    myInt=count_ones(i);
    printf("%d\r\n",myInt);
    }
    //A simple example of C code, designed to count the 1 bits in a int, might look like this:
    
    int count_ones(unsigned int x) {
        int result = 0;
        while (x != 0)
           result++, x = x & (x-1);
        return result;
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Every time you do this x=x&(x-1) it sets the rightmost 1-bit to 0.
    Code:
    x        00100101
    x-1      00100100
    x&(x-1)  00100100     rightmost bit gone
    
    x        00100100
    x-1      00100011
    x&(x-1)  00100000     rightmost bit gone
    
    x        00100000
    x-1      00011111
    x&(x-1)  00000000     now x is 0, so we're done
    Subtracting 1 from a number sets it's rightmost 1-bit to 0 and any 0 bits to the right of it to 1's. The "bitwise and" masks the extra 1's against the 0's in the original number.

    Your variables don't need to be global.
    You should have the function prototype above main.
    You should generally avoid using the comma operator to avoid braces.
    You probably don't need to print the '\r' character.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Registered User
    Join Date Jul 2002
    Posts 1

    Now that's some quality lurking
    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.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    33
    Hi John, thanks for taking the time to answer my question. I see what's happening now. Isn't there supposed to be a semicolon after result++ ?
    That is what i don't understand. But the C compiler that I am using takes it fine.
    Last edited by joemccarr; 01-24-2018 at 06:21 PM.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    33
    Quote Originally Posted by Salem View Post
    Registered User
    Join Date Jul 2002
    Posts 1

    Now that's some quality lurking
    Wow, is that when I signed up ? I haven't been lurking but I should of been. I must of been on the Arduino forum
    Last edited by joemccarr; 01-24-2018 at 06:24 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The comma operator makes it a single statement, so you can get away without using braces in the while loop.

    This is the same, only better.
    Code:
    while (x != 0){
           result++;
           x = x & (x-1);
    }
    You can now add say a printf to examine variables without having to completely restructure the code.
    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.

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    33
    Ok, thanks Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'If' Statement inside the Switch statement being ignored.
    By howardbc14 in forum C Programming
    Replies: 4
    Last Post: 04-11-2015, 11:45 AM
  2. Help With If/Else If Statement
    By ThePhoenixRisin in forum C Programming
    Replies: 2
    Last Post: 11-05-2012, 04:40 AM
  3. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 03:18 PM
  4. If Statement
    By boontune in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2003, 07:56 AM
  5. if statement
    By ZakkWylde969 in forum C++ Programming
    Replies: 22
    Last Post: 07-11-2003, 10:48 PM

Tags for this Thread