Thread: Bitwise Operators - Setting and Retreiving a Bit

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    2

    Bitwise Operators - Setting and Retreiving a Bit

    I need to create a function that will set the ith bit of its argument. It should do nothing if it is out of range. The function prototype should be as follows:

    unsigned long set_bit(unsigned long x, int i, int bit);

    I must prompt the user for the desired bit, then prompt the user for value of the bit (0 or 1), perform the operation, and then display the changed operand (x) in 8 digit HEX format.

    This is part of a larger project involving bitwise operators. I've made one other function in the project that is supposed to return a specific bit of it's argument. Although, it does not work, and I can't figure out why. Here's my code:

    Code:
    #include <stdio.h>
    
    int get_bit(unsigned long int x, int i)
    {
    	if((x & (1 << (i - 1))) == 1)
    		return 1;
    	else
    		return 0;
    }
    
    void main()
    {
    	unsigned long int x;
    	int i;
    	printf("Input a number: ");
    	scanf("%ul", &x);
    	printf("Input which bit of the number you want: ");
    	scanf("%d", &i);
    	if (get_bit(x, i) == 1)
    		printf("1\n\n");
    	else
    		printf("0\n\n");
    }
    Any help on either of my problems would be greatly appreciated.

  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
    > if((x & (1 << (i - 1))) == 1)
    Should be
    if((x & (1 << (i - 1))) == (1<<(i-1)) )

    The bit you extract with the left hand side does not get shifted to the least significant bit

    Though it's usually easier to say
    if((x & (1 << (i - 1))) != 0 )

    Oh, and never use void main - see the FAQ
    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
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Bitwise work can be confusing if you don't have something like below to clear up the syntax of picking out individual bit values.
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    #define BIT(n) ( 1UL << ( n ) )
    
    int get_bit(unsigned long x, int i)
    {
      return !!( x & BIT ( i ) );
    }
    
    unsigned long set_bit(unsigned long x, int i, int bit)
    {
      if ( i < sizeof x * CHAR_BIT && i >= 0 ) {
        if ( bit ) /* Set the bit */
          x = ( get_bit ( x, i ) ) ? x : x | BIT ( i );
        else       /* Clear the bit */
          x = ( get_bit ( x, i ) ) ? x &= ~BIT ( i ) : x;
      }
      
      return x;
    }
    
    int main ( void )
    {
      int i = 0;
      
      printf ( "Bit 0: %d\n", get_bit ( i, 0 ) );
      i = set_bit ( i, 0, 1 );
      printf ( "Set 0: %d\n", i );
      printf ( "Bit 1: %d\n", get_bit ( i, 1 ) );
      i = set_bit ( i, 1, 1 );
      printf ( "Set 1: %d\n", i );
      printf ( "Bit 3: %d\n", get_bit ( i, 2 ) );
      i = 0;
      i = set_bit ( i, sizeof i * CHAR_BIT, 1 );
      printf ( "i too big:   %d\n", i );
      i = 0;
      i = set_bit ( i, -1, 1 );
      printf ( "i too small: %d\n", i );
      
      return 0;
    }
    It's still probably confusing, but better than it would be if you hard coded ( 1UL << i ) everywhere.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed