bit operations

Discussion in 'C++' started by Ajay, Aug 3, 2005.

  1. Ajay

    Ajay Guest

    Hi All

    Well then.. it could also be posted to C group... but I am subscribed
    to this group so thought of posting here.

    1. Given a number X, how do you make nth bit (X is integer, so 1<n<32)
    on?
    2. Given a number X, how can you check if nth bit is on or not? (You
    can not destroy the number)


    It is not colleger assignment. Working for last 7 years.. :)... today
    we were discussing about storing the array traversal & storing the
    number of element we are interested in most efficient way. Somebody
    suggested - use another array (but that menas too much memory). so we
    thought of having a number num=0 in the beginning and and the which
    ever element we are interested in we'll just store the location using
    that numbered bit in the array.

    Array will have at most 32 elements.
    Ajay, Aug 3, 2005
    #1
    1. Advertising

  2. * Ajay:
    >
    > Well then.. it could also be posted to C group... but I am subscribed
    > to this group so thought of posting here.
    >
    > 1. Given a number X, how do you make nth bit (X is integer, so 1<n<32)
    > on?
    > 2. Given a number X, how can you check if nth bit is on or not? (You
    > can not destroy the number)
    >
    >
    > It is not colleger assignment. Working for last 7 years.. :)...


    In that case, get a new job.

    And learn to spell.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Aug 3, 2005
    #2
    1. Advertising

  3. Ajay

    WittyGuy Guest

    Ajay wrote:
    > 1. Given a number X, how do you make nth bit (X is integer, so 1<n<32)
    > on?
    > 2. Given a number X, how can you check if nth bit is on or not? (You
    > can not destroy the number)


    Do you know the value of n? If n is known then blindly do bitwise OR
    operation leaving other bits as 0. To check do AND operation and check
    for the resulting value.

    As simple as eating cheese ;-)

    -Wg-
    WittyGuy, Aug 3, 2005
    #3
  4. Ajay

    Chris Theis Guest

    "Ajay" <> wrote in message
    news:...
    > Hi All
    >
    > Well then.. it could also be posted to C group... but I am subscribed
    > to this group so thought of posting here.
    >
    > 1. Given a number X, how do you make nth bit (X is integer, so 1<n<32)
    > on?
    > 2. Given a number X, how can you check if nth bit is on or not? (You
    > can not destroy the number)
    >
    >
    > It is not colleger assignment. Working for last 7 years.. :)... today
    > we were discussing about storing the array traversal & storing the
    > number of element we are interested in most efficient way. Somebody
    > suggested - use another array (but that menas too much memory). so we
    > thought of having a number num=0 in the beginning and and the which
    > ever element we are interested in we'll just store the location using
    > that numbered bit in the array.
    >
    > Array will have at most 32 elements.
    >


    You can use the binary OR to set a bit like this:

    X |= ( 1 << BitNr );

    If you want to clear it you can do this via:

    X = X &~( 1 << BitNr );

    To test whether the bit is set you will use the binary AND:

    if( X & ( 1 << BitNr ) )
    return true;

    Howevery you should make sure that BitNr represents a reasonable value.

    On the other hand you could also use a vector<bool> for a dynamically
    growing bit array or a bitset for fixed-sized arrays of bits.

    HTH
    Chris
    Chris Theis, Aug 3, 2005
    #4
  5. Ajay

    Guest

    Ajay wrote:
    > Hi All
    >
    > Well then.. it could also be posted to C group... but I am subscribed
    > to this group so thought of posting here.
    >
    > 1. Given a number X, how do you make nth bit (X is integer, so 1<n<32)
    > on?


    I assum you meant 1 < = n < = 32

    int Shift = 1;
    Shift <<= (N-1);
    X |= Shift;

    > 2. Given a number X, how can you check if nth bit is on or not? (You
    > can not destroy the number)


    int Shift = 1;
    Shift <<= (N-1);
    if ( 0 != (X & Shift))
    {
    std::cout << "Bit " << N << " is on" << std::endl;
    }
    , Aug 3, 2005
    #5
  6. Ajay

    Ajay Guest

    So

    if (person in your company someone makes a spelling mistake && has a
    bug against his name)
    fire that person
    endif

    right?

    Alf P. Steinbach wrote:
    > * Ajay:
    > >
    > > Well then.. it could also be posted to C group... but I am subscribed
    > > to this group so thought of posting here.
    > >
    > > 1. Given a number X, how do you make nth bit (X is integer, so 1<n<32)
    > > on?
    > > 2. Given a number X, how can you check if nth bit is on or not? (You
    > > can not destroy the number)
    > >
    > >
    > > It is not colleger assignment. Working for last 7 years.. :)...

    >
    > In that case, get a new job.
    >
    > And learn to spell.
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?
    Ajay, Aug 3, 2005
    #6
  7. * Ajay:
    > [top-posting]
    > [quoting signature]


    Please do not top-post in this group. See the FAQ. Corrected.

    Please do not quote irrelevant material such as signatures. Corrected.


    * Ajay:
    > * Alf P. Steinbach:
    > > * Ajay:
    > > >
    > > > Well then.. it could also be posted to C group... but I am subscribed
    > > > to this group so thought of posting here.
    > > >
    > > > 1. Given a number X, how do you make nth bit (X is integer, so 1<n<32)
    > > > on?
    > > > 2. Given a number X, how can you check if nth bit is on or not? (You
    > > > can not destroy the number)
    > > >
    > > >
    > > > It is not colleger assignment. Working for last 7 years.. :)...

    > >
    > > In that case, get a new job.
    > >
    > > And learn to spell.

    >
    > So
    >
    > if (person in your company someone makes a spelling mistake && has a
    > bug against his name)
    > fire that person
    > endif
    >
    > right?


    When you haven't learned the basics after 7 years in the job, find another
    job.

    What I strongly suspect, however, is that you're lying, and is a high-school
    student.

    That's both because it seems impossible to be so incompetent, and because of
    the spelling errors and behavior which indicate at most high-school level.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Aug 3, 2005
    #7
  8. Ajay

    Ajay Guest

    Thanks everybody.. that was simple..

    Btw, Alf, thanks to you too. because I really had forgotten the basics
    that I should always check for correct spellings before sending any
    email/group post.

    about basics - sometimes a person can overlook simplest of things..
    :(.. can heppen with anybody..

    me working/not working/high school - honestly your openion does not
    matter. It does not help either. Only solution would have helped here.
    Ajay, Aug 3, 2005
    #8
  9. * Ajay:
    > Thanks everybody.. that was simple..
    >
    > Btw, Alf, thanks to you too. because I really had forgotten the basics
    > that I should always check for correct spellings before sending any
    > email/group post.
    >
    > about basics - sometimes a person can overlook simplest of things..
    > :(.. can heppen with anybody..
    >
    > me working/not working/high school - honestly your openion does not
    > matter. It does not help either. Only solution would have helped here.


    The group has a policy of not doing people's homework.

    For good reason: you don't learn anything that way (including that you don't
    learn how to find out), and we or someone may end up with a clueless you on
    a project.

    See the FAQ:
    <url: http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2>.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Aug 3, 2005
    #9
  10. Ajay

    Default User Guest

    Ajay wrote:


    > 1. Given a number X, how do you make nth bit (X is integer, so 1<n<32)
    > on?


    How did you arrive at this conclusion? There are a number of integer
    types in C++, none of which are guaranteed to be exactly 32 bits
    (assuming you actually meant to write 1 <= N <= 32).

    The built-in type int is often 32 bits, but is not required to be so.
    It is required to be at least 16 bits.




    Brian
    Default User, Aug 3, 2005
    #10
    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. Sanket Suryawanshi

    bit operations in Java

    Sanket Suryawanshi, Jan 23, 2004, in forum: Java
    Replies:
    3
    Views:
    7,073
    Jon A. Cruz
    Jan 23, 2004
  2. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,154
    robert
    Feb 11, 2006
  3. Replies:
    3
    Views:
    1,743
    Timothy Bendfelt
    Jan 19, 2007
  4. Replies:
    9
    Views:
    968
    Juha Nieminen
    Aug 22, 2007
  5. Jeff.M
    Replies:
    6
    Views:
    172
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...

Share This Page