bit value generation doubt

This is a discussion on bit value generation doubt within the C Programming forums, part of the General Programming Boards category; I came across this link on how to generate the bitwise represenation of any int , char on one of ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    bit value generation doubt

    I came across this link on how to generate the bitwise represenation of any int , char on one of the forums.

    showbits

    The logic that the program uses is

    [insert]
    Code:
    void bits_uint(unsigned int value)
     {
        unsigned int bit;
        for ( bit = /* msb */(~0U >> 1) + 1; bit > 0; bit >>= 1 )
        {
          putchar(value & bit ? '1' : '0');
        } 
       putchar('\n');
    }
    In the for loop the condition that the program uses is

    for ( bit = /* msb */(~0U >> 1) + 1; bit > 0; bit >>= 1 )

    What does this imply

    bit >>= 1(greater than , greater than equal to ???????????????)


    If i understand it now its supposed to mean

    bit = bit >> 1;
    Last edited by roaan; 08-07-2009 at 02:49 PM. Reason: understood the meaning

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    Well it's like += say.

    And you are correct.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    bit = /* msb */(~0U >> 1) + 1;

    Could also be written:

    bit = /* msb */~(~0U>>1);

    Which that makes slightly more obvious sense to me, since what you want is just the most significant bit set (eg, "1000000000"), which would be all bits set (~0), shift right once ("01111111111") and flip that (~).

    By ANDing that with the value each time bit >>= 1, you can saying whether the corresponding bit in value is set.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-10-2005, 10:53 AM
  2. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 10:20 AM
  3. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 11:54 AM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-26-2001, 12:45 AM
  5. bit conversions
    By wazilian in forum C Programming
    Replies: 4
    Last Post: 10-25-2001, 09:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21