Thread: 2's complement

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    62

    2's complement

    I am following one of the exercises from C Programming language book:
    The C Programming Language Exercise 2-9

    Now as I am playing around with this, I notice something strange:

    Code:
    #include <stdio.h> 
     
    #define BUF_SIZE 33 
     
    char *int2bin(int a, char *buffer, int buf_size) { 
     
        buffer += (buf_size - 1); 
     
        int i; 
        for (i = 31; i >= 0; i--) { 
     
            *buffer-- = (a & 1) + '0'; 
     
            a >>= 1; 
        } 
     
     
        return buffer; 
    } 
     
     
     
    int main(void) { 
     
        char buffer[BUF_SIZE]; 
     
        buffer[BUF_SIZE - 1] = '\0'; 
    
        /* xx here is even */
        int xx = 0x4; 
        int2bin(xx, buffer, BUF_SIZE - 1); 
        printf("a = %s\n", buffer);
    
        xx &= (xx-1); 
        int2bin(xx, buffer, BUF_SIZE – 1); 
    
        printf("a = %s\n", buffer); 
        return 0; 
    }
    This program returns this:

    a = 00000000000000000000000000000100
    a = 00000000000000000000000000000000

    I understand why it happens. 00000100 is binary representation of 4. 00000011 is binary representation of 3. When we use AND-operator on these two, we get 00000000.

    Notice how the solution states what AND-operator will do:

    If x is even, then the representation of (x-1) has the rightmost zeros of x becoming ones and the rightmost one becoming a zero. Anding the two clears the rightmost 1-bit in x and all the rightmost 1-bits from (x-1).

    Ok, but what is the purpose of all this? What are we trying to prove here? I don't understand what's the point of this exercise.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You posted the point of the exercise in your question: "Clears the rightmost 1-bit in x".

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    62
    One thing though if the integer is even, the rightmost bit is always cleared in x. There is no even number with a 1 at the rightmost bit.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Every number except zero has a rightmost 1-bit, because every number except zero has at least one 1-bit, and one of them must be farthest to the right. (Notice that we are not (necessarily) talking about bit zero, which is only set for odd numbers, but the rightmost bit that is set.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two's complement
    By matthayzon89 in forum Tech Board
    Replies: 7
    Last Post: 08-30-2010, 12:22 AM
  2. complement ~
    By Tool in forum C Programming
    Replies: 4
    Last Post: 03-17-2010, 09:04 AM
  3. two's complement
    By alyeska in forum C++ Programming
    Replies: 4
    Last Post: 10-21-2007, 09:55 PM
  4. 1's and 2's complement
    By Jaguar in forum C Programming
    Replies: 3
    Last Post: 10-14-2003, 09:12 AM
  5. how to complement
    By stautze in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2002, 06:16 AM