Efficiently generating bits

Discussion in 'C Programming' started by nish.sinnadurai@gmail.com, Jul 16, 2006.

  1. Guest

    Hi,

    I have two code snippets here and I'm wondering which is better, and
    whether or not there is a better way to do the following:

    I want to fill in an unsigned int variable with n 1s. Example: n = 4
    --> var = 00...001111 in binary.

    So,

    Method 1:

    var = 0;
    var = pow(2, n) - 0.9999;

    Method 2:

    var = 0;
    for(i = 0; i < n; i++)
    var = var << 1 + 1;

    ---

    I'm not quite sure which method is better (wrt to computational time),
    or if there is a more elegant way to do this.

    Can anyone help me out here?


    Thanks!
    Nish

    UW09
    , Jul 16, 2006
    #1
    1. Advertising

  2. Ben Pfaff Guest

    writes:

    > I want to fill in an unsigned int variable with n 1s. Example: n = 4
    > --> var = 00...001111 in binary.


    If n is less than the number of value bits in an unsigned int:
    x = (1u << n) - 1;
    If n might be (greater than or) equal to the number of value
    bits:
    x = n < NUM_VALUE_BITS ? (1u << n) - 1 : UINT_MAX;
    where NUM_VALUE_BITS is the width of unsigned int, which is
    typically CHAR_BIT * sizeof (unsigned int) but might be smaller.
    --
    "If I've told you once, I've told you LLONG_MAX times not to
    exaggerate."
    --Jack Klein
    Ben Pfaff, Jul 16, 2006
    #2
    1. Advertising

  3. Guest

    Ben Pfaff wrote:
    > writes:
    >
    > > I want to fill in an unsigned int variable with n 1s. Example: n = 4
    > > --> var = 00...001111 in binary.

    >
    > If n is less than the number of value bits in an unsigned int:
    > x = (1u << n) - 1;



    This method is really good.

    Soon after I posted I thought of:

    var = ~((~0u) << n);

    but yours seems to be the best solution.


    Thanks!
    Nish
    , Jul 16, 2006
    #3
  4. Guest

    wrote:

    > Hi,
    >
    > I have two code snippets here and I'm wondering which is better, and
    > whether or not there is a better way to do the following:
    >
    > I want to fill in an unsigned int variable with n 1s. Example: n = 4
    > --> var = 00...001111 in binary.
    >
    > So,
    >
    > Method 1:
    >
    > var = 0;
    > var = pow(2, n) - 0.9999;
    >
    > Method 2:
    >
    > var = 0;
    > for(i = 0; i < n; i++)
    > var = var << 1 + 1;


    Is var a double or int ?
    , Jul 16, 2006
    #4
    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,523
    Donar
    Jul 6, 2006
  2. Rajarshi Guha

    generating points on a grid - efficiently

    Rajarshi Guha, Mar 6, 2004, in forum: Python
    Replies:
    2
    Views:
    260
    Rajarshi Guha
    Mar 6, 2004
  3. sarmin kho
    Replies:
    2
    Views:
    820
    A. Lloyd Flanagan
    Jun 15, 2004
  4. Miki Tebeka
    Replies:
    1
    Views:
    435
    Marcin 'Qrczak' Kowalczyk
    Jun 14, 2004
  5. random bits efficiently

    , Sep 21, 2009, in forum: Perl Misc
    Replies:
    1
    Views:
    99
    Danny Woods
    Sep 21, 2009
Loading...

Share This Page