bitset in C

Discussion in 'C++' started by rudra, Jul 9, 2008.

  1. rudra

    rudra Guest

    i have a C++ program

    #include <bitset>
    #include <iostream>

    int main()
    {
    int i = 23356;
    int j=~i;
    printf("One's complement of i=%u is ~i=%u\n",i,~i);
    std::cout << std::bitset<32>(i);
    printf("\n");
    std::cout << std::bitset<32>(j);
    }

    is there any equivalent way of bitset in C? or easier way of doing the
    same thing in C?
    rudra, Jul 9, 2008
    #1
    1. Advertising

  2. rudra wrote:
    > is there any equivalent way of bitset in C?


    As a general rule, the C library doesn't offer *any* utility functions
    or data containers, except those related to I/O and some string
    manipulation.
    (The only exception is qsort(), which in itself is slower than
    std::sort().)
    Juha Nieminen, Jul 9, 2008
    #2
    1. Advertising

  3. rudra

    Guest

    On Jul 9, 9:29 am, rudra <> wrote:
    > is there any equivalent way of bitset in C?

    The int is good enough for most of the C developers.

    > or easier way of doing the same thing in C?

    Printing the bits of integer? The C developers tend to prefer hex
    format. Anyway, printing bits does not take much.

    #include <stdio.h>

    void print_bits(int n)
    {
    unsigned bits = 8 * sizeof n;

    while(bits--)
    putchar('0' + (n >> bits & 1));
    }

    int main()
    {
    int i = 23356;
    int j=~i;
    printf("One's complement of i=%u is ~i=%u\n",i,~i);
    print_bits(i);
    printf("\n");
    print_bits(j);
    }

    Regards,
    Vladimir
    , Jul 9, 2008
    #3
  4. rudra

    Guest

    On Jul 9, 6:08 pm, Victor Bazarov <> wrote:
    >      ? (CHAR_BIT comes from <limits.h>)


    Good point, thanks!

    Regards,
    Vladimir
    , Jul 10, 2008
    #4
  5. rudra

    Lionel B Guest

    On Wed, 09 Jul 2008 10:02:25 -0700, vova777 wrote:

    > On Jul 9, 9:29 am, rudra <> wrote:
    >> is there any equivalent way of bitset in C?


    Not in general... see below.

    > The int is good enough for most of the C developers.


    A std::bitset can be an arbitrary (compile-time constant) number of bits.
    Maybe not a concern for the OP if they only want 32 bits and/or all they
    need to do is print them out. Full std::bitset functionality will be much
    more hassle to achieve in C - basically you rewrite the internals of
    std::bitset, except that you're not allowed to use templates ;-)

    --
    Lionel B
    Lionel B, Jul 10, 2008
    #5
    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. Timo Nentwig

    Performance of java.util.BitSet

    Timo Nentwig, Dec 9, 2003, in forum: Java
    Replies:
    4
    Views:
    3,897
  2. Joshua
    Replies:
    4
    Views:
    377
    Esmond Pitt
    May 6, 2005
  3. Dill Hole
    Replies:
    2
    Views:
    429
    Mike Wahler
    Jul 5, 2003
  4. Victor Bazarov

    Re: bitset to UCHAR

    Victor Bazarov, Jul 8, 2003, in forum: C++
    Replies:
    1
    Views:
    539
    voger
    Jul 8, 2003
  5. Ninds
    Replies:
    14
    Views:
    701
    W Karas
    Dec 3, 2012
Loading...

Share This Page