counting of bits

Discussion in 'C Programming' started by rajm2019, May 26, 2007.

  1. rajm2019

    rajm2019 Guest

    Given two integers A & B. Determine how many bits required to convert
    A to B.how to write a function int BitSwapReqd(int A, int B);
     
    rajm2019, May 26, 2007
    #1
    1. Advertisements

  2. rajm2019

    osmium Guest

    The exclusive or operator may help you.
     
    osmium, May 26, 2007
    #2
    1. Advertisements

  3. rajm2019

    Don Bruder Guest

    Why do I get the feeling someone has a homework assignment, and hasn't
    been paying attention in class?

    (CF his other post - "function to divide by three")
     
    Don Bruder, May 26, 2007
    #3
  4. rajm2019

    rajm2019 Guest

    how to do this pls help me
     
    rajm2019, May 26, 2007
    #4
  5. rajm2019

    Default User Guest


    Sounds like your instructor would like you to write a C program. Better
    get going.




    Brian
     
    Default User, May 26, 2007
    #5
  6. rajm2019

    Don Bruder Guest

    Sure... I can do your homework for you - I charge US$500.00 per half
    hour, with a two hour minimum, and expect payment in advance.

    Alternatively, you could pay attention in class.

    (Hint: If you'd been paying attention, rather than screwing off, you'd
    be able to whip out any of these assignments in about 15 minutes, tops.
    These are all trivial stuff that anyone can deal with easily - IF they
    pay attention in class instead of screwing around.)
     
    Don Bruder, May 26, 2007
    #6
  7. integers just -are-, independant of representation. Bits are
    a particular representation, and there are an infinite number
    of bit representations of any given integer.
    Note that an 'int' is not an integer: int is only an implementation-
    defined subset of integers.

    Is there supposed to be a connection between the 'Swap' in the
    function name and the earlier requirement about "convert A to B" ?
    Swapping implies the exchange of two values, while "convert"
    only implies the transformation of one value; the two verbs could
    come out with very different results.
     
    Walter Roberson, May 26, 2007
    #7
  8. rajm2019

    Thad Smith Guest

    The number of bits required depends on the definition of conversion in
    this instance. What definition are you using?
     
    Thad Smith, May 27, 2007
    #8
  9. Guess the homework must have been due by now :)

    I think it's pretty clear the OP wants to find the "hamming distance"
    between the base 2 expression of two integers... I'd do that like
    this:

    unsigned int hd(unsigned int a, unsigned int b)
    {
    unsigned int c=a^b, d=0;
    while(c) {
    d+=(c & 1);
    c>>=1;
    }
    return d;
    }

    For signed integers, you'd need to worry about how the integer is
    being represented - 1s/2s complement etc.

     
    Francine.Neary, May 27, 2007
    #9
  10. With slight stylistic improvements (unnecessary auto variable and
    superfluous parentheses removed):

    unsigned int hd(unsigned int a, unsigned int b)
    {
    unsigned int d=0;
    a^=b;
    while(a) {
    d+=a & 1;
    a>>=1;
    }
    return d;
    }
     
    Francine.Neary, May 27, 2007
    #10
  11. rajm2019

    pete Guest

    I write that, this way:

    while (a != 0) {
    ++d;
    a &= a - 1;
    }
     
    pete, May 28, 2007
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.