Thread: viewing and setting bits on a number

  1. #1
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95

    viewing and setting bits on a number

    I am working on a problem, which asked for a program to view and set different bits of a number. Here is what I have. Can this be improved in any way? I already know about the bit fields; the author asked to do it this way.

    Code:
    #include <stdio.h>
    unsigned int bit_test( unsigned int value, int n)
    {
      //value <<=  n;
      value = (value >> n) & 0x01;
      return value;
    }
    unsigned bit_set ( unsigned int value, int n)
    {
      value |= 1 << n;
      return value;
    }
    void show_bits(unsigned int value)
    {
      int i;
      for (i=0; i<32; i++) // Create a sepaate function for this
      {
        bit_test(value, i);
        printf ("The value of bit #%d for the number %d is:%d\n", i, value,bit_test(value, i));
      }
    }
    int main (void)
    {
    unsigned int value;
    int i, n;
    n =3;
    value = 5577;
    for (i=0; i<32; i++) // Create a sepaate function for this
    {
      bit_test(value, i);
      printf ("The value of bit #%d for the number %d is:%d\n", i, value,bit_test(value, i));
    }
    printf("\nIf you want to set one of the bits of value, type in the bit number (0 - 31)");
    scanf("%d",&n);
    value = bit_set (value, n);
    show_bits(value);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you include limits.h, you can improve things by writing
    for (i=0; i<CHAR_BIT*sizeof(value); i++)
    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 zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    Thanks.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You can also find the high bit with some math and just use that to control your loop. The cast here is important.

    Another tip, especially if you want nice spacing, is to handle each byte separately.
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    
    #define MSB ((unsigned long)-1 / 2 + 1)
    
    unsigned long b;
    for (b = MSB; b > 0; b >>= 1) ...
    Last edited by whiteflags; 06-09-2016 at 03:57 AM.

  5. #5
    Registered User zolfaghar's Avatar
    Join Date
    Mar 2016
    Posts
    95
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Viewing and Counting Set Bits in an Integer
    By Adak in forum C Programming
    Replies: 9
    Last Post: 08-13-2010, 07:05 PM
  2. setting the values of bits
    By yahn in forum C++ Programming
    Replies: 14
    Last Post: 04-14-2008, 12:09 AM
  3. Setting bits
    By Buckshot in forum C++ Programming
    Replies: 15
    Last Post: 06-06-2005, 12:37 PM
  4. Setting bits
    By Kinasz in forum C Programming
    Replies: 9
    Last Post: 07-05-2004, 05:12 AM
  5. copy some bits into a 8 bits binary number
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 05-29-2002, 10:54 AM

Tags for this Thread