bits required

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

  1. Guest

    count the bits required to be altered while swaping values a and b
     
    , May 26, 2007
    #1
    1. Advertising

  2. said:

    > count the bits required to be altered while swaping values a and b


    Why?

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, May 26, 2007
    #2
    1. Advertising

  3. Default User Guest

    wrote:

    > count the bits required to be altered while swaping values a and b



    You can repost it all you want, we still won't do your homework for
    you.

    Back in the day, when I was a teaching assistant (Physics), I used to
    tell the students, "It's pointless to cheat on homework. Homework is
    for you. We only collect and grade it so you'll get feedback on what
    you're doing wrong and to give you incentive to actually do the work.
    Exams are where we find out what you really know."




    Brian
     
    Default User, May 26, 2007
    #3
  4. In article <>,
    <> wrote:

    >count the bits required to be altered while swaping values a and b


    The answer may be ill-defined. If a temporary value is used,
    the the number of bits required to be altered to set that
    temporary to a certain value would depend on what was in
    the temporary to start with, which is unknown if the temporary
    is in automatic storage.

    There is also the question of how to count the alteration of bits.
    If a particular bit gets altered twice, then is that a count of 2
    (two alterations), or a count of 1 (one bit altered) ?

    Is the requirement to come up with a formula that expresses the
    number of bits required to be altered to swap two values? Or is
    the requirement to come up with a program that would count the
    bits required to be altered to swap two values that are inputs?
    Or are the two values given ahead of time?

    What do we know about the types of the two values a and b?
    Are they integral types? Are they potentially floating point
    numbers? Are they potentially of mixed type, one integral
    and one floating point?

    What are we to do about the problem that "bits" are a matter
    of representation (which is not completely defined in C),
    whereas "values" are independant of representation? For example,
    the answer might be different if the C implementation happens
    to be using two's complement to represent values when the programmer
    is expecting one's complement representation instead.
    --
    Programming is what happens while you're busy making other plans.
     
    Walter Roberson, May 26, 2007
    #4
  5. On 26 May 2007 09:11:23 -0700, wrote:

    >count the bits required to be altered while swaping values a and b


    Posting the same question every hour will not get you more or faster
    responses. However, it will increase the number of killfiles that
    contain you name.

    Posting an obvious homework question while making no attempt to solve
    it yourself merely hastens the process.


    Remove del for email
     
    Barry Schwarz, May 26, 2007
    #5
  6. Flash Gordon Guest

    wrote:
    > count the bits required to be altered while swaping values a and b


    42.7
    --
    Flash Gordon
     
    Flash Gordon, May 26, 2007
    #6
  7. Barry Schwarz said:

    > On 26 May 2007 09:11:23 -0700, wrote:
    >
    >>count the bits required to be altered while swaping values a and b

    >
    > Posting the same question every hour will not get you more or faster
    > responses. However, it will increase the number of killfiles that
    > contain you name.


    Indeed. In fact, from over here it's already too late.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, May 26, 2007
    #7
  8. writes:
    > count the bits required to be altered while swaping values a and b


    Give us your instructor's e-mail address so we can submit the solution
    directly.

    Optionally, you can also give us your name so we can tell your
    instructor who should get the "credit".

    Or you might just consider doing your own homework.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 26, 2007
    #8
  9. pete Guest

    wrote:
    >
    > count the bits required to be altered while swaping values a and b


    /* BEGIN new.c */

    #include <stdio.h>
    #include <stdlib.h>

    unsigned bit_count(unsigned n);

    int main(void)
    {
    unsigned a, b, mask;

    a = rand();
    b = rand();
    mask = a ^ b;
    printf("a is %u\nb is %u.\n", a, b);
    printf("%u bits would need to be flipped in each object\n"
    "to swap their values.\n", bit_count(mask));
    printf("%u ^ %u is %u\n", a, mask, a ^ mask);
    printf("%u ^ %u is %u\n", b, mask, b ^ mask);
    return 0;
    }

    unsigned bit_count(unsigned n)
    {
    unsigned count;

    for (count = 0; n != 0; n &= n - 1) {
    ++count;
    }
    return count;
    }

    /* END new.c */

    --
    pete
     
    pete, May 26, 2007
    #9
  10. Shraddha Guest

    On May 27, 3:52 am, pete <> wrote:
    > wrote:
    >
    > > count the bits required to be altered while swaping values a and b

    >
    > /* BEGIN new.c */
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > unsigned bit_count(unsigned n);
    >
    > int main(void)
    > {
    > unsigned a, b, mask;
    >
    > a = rand();
    > b = rand();
    > mask = a ^ b;
    > printf("a is %u\nb is %u.\n", a, b);
    > printf("%u bits would need to be flipped in each object\n"
    > "to swap their values.\n", bit_count(mask));
    > printf("%u ^ %u is %u\n", a, mask, a ^ mask);
    > printf("%u ^ %u is %u\n", b, mask, b ^ mask);
    > return 0;
    >
    > }
    >
    > unsigned bit_count(unsigned n)
    > {
    > unsigned count;
    >
    > for (count = 0; n != 0; n &= n - 1) {
    > ++count;
    > }
    > return count;
    >
    > }
    >
    > /* END new.c */
    >
    > --
    > pete


    Why you are using unsigned int everywhere?Why count is unsigned?And
    why you XOR the inputs?
     
    Shraddha, May 27, 2007
    #10
  11. pete Guest

    Shraddha wrote:
    >
    > On May 27, 3:52 am, pete <> wrote:
    > > wrote:
    > >
    > > > count the bits required to be altered while swaping values a and b

    > >
    > > /* BEGIN new.c */
    > >
    > > #include <stdio.h>
    > > #include <stdlib.h>
    > >
    > > unsigned bit_count(unsigned n);
    > >
    > > int main(void)
    > > {
    > > unsigned a, b, mask;
    > >
    > > a = rand();
    > > b = rand();
    > > mask = a ^ b;
    > > printf("a is %u\nb is %u.\n", a, b);
    > > printf("%u bits would need to be flipped in each object\n"
    > > "to swap their values.\n", bit_count(mask));
    > > printf("%u ^ %u is %u\n", a, mask, a ^ mask);
    > > printf("%u ^ %u is %u\n", b, mask, b ^ mask);
    > > return 0;
    > >
    > > }
    > >
    > > unsigned bit_count(unsigned n)
    > > {
    > > unsigned count;
    > >
    > > for (count = 0; n != 0; n &= n - 1) {
    > > ++count;
    > > }
    > > return count;
    > >
    > > }
    > >
    > > /* END new.c */


    > Why you are using unsigned int everywhere?
    > Why count is unsigned?


    Bitwise operations are better suited to unsigned types.

    > And why you XOR the inputs?


    The result of a ^ b,
    is a mask which has the bits set,
    where a and b differ.

    --
    pete
     
    pete, May 27, 2007
    #11
  12. "Shraddha" <> schrieb im Newsbeitrag
    news:...
    > On May 27, 3:52 am, pete <> wrote:
    >> wrote:
    >>
    >> > count the bits required to be altered while swaping values a and b

    >>
    >> /* BEGIN new.c */
    >>
    >> #include <stdio.h>
    >> #include <stdlib.h>
    >>
    >> unsigned bit_count(unsigned n);
    >>
    >> int main(void)
    >> {
    >> unsigned a, b, mask;
    >>
    >> a = rand();
    >> b = rand();
    >> mask = a ^ b;
    >> printf("a is %u\nb is %u.\n", a, b);
    >> printf("%u bits would need to be flipped in each object\n"
    >> "to swap their values.\n", bit_count(mask));
    >> printf("%u ^ %u is %u\n", a, mask, a ^ mask);
    >> printf("%u ^ %u is %u\n", b, mask, b ^ mask);
    >> return 0;
    >>
    >> }
    >>
    >> unsigned bit_count(unsigned n)
    >> {
    >> unsigned count;
    >>
    >> for (count = 0; n != 0; n &= n - 1) {
    >> ++count;
    >> }
    >> return count;
    >>
    >> }
    >>
    >> /* END new.c */
    >>
    >> --
    >> pete

    >
    > Why you are using unsigned int everywhere?

    To avoid a leading bit to be interpreted as a negative value

    > Why count is unsigned?

    Why not? After all there would never be a negative number of bits counted.
    unsigned char would have been sufficient here, don't think there are many
    machine out there with more than 256 bits in an int...

    > And why you XOR the inputs?

    To find the bits that are set differently in the 2 operands

    Bye, Jojo
     
    Joachim Schmitz, May 27, 2007
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GGG
    Replies:
    10
    Views:
    12,568
    Donar
    Jul 6, 2006
  2. sarmin kho
    Replies:
    2
    Views:
    828
    A. Lloyd Flanagan
    Jun 15, 2004
  3. Miki Tebeka
    Replies:
    1
    Views:
    446
    Marcin 'Qrczak' Kowalczyk
    Jun 14, 2004
  4. sergey

    "casting" bits to bits?

    sergey, Nov 8, 2006, in forum: VHDL
    Replies:
    1
    Views:
    710
    sergey
    Nov 8, 2006
  5. Tomás

    Value Bits Vs Object Bits

    Tomás, Jun 2, 2006, in forum: C Programming
    Replies:
    13
    Views:
    549
    Hallvard B Furuseth
    Jul 1, 2006
Loading...

Share This Page