Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > bitset in C

Reply
Thread Tools

bitset in C

 
 
rudra
Guest
Posts: n/a
 
      07-09-2008
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?
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      07-09-2008
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().)
 
Reply With Quote
 
 
 
 
vova777@gmail.com
Guest
Posts: n/a
 
      07-09-2008
On Jul 9, 9:29*am, rudra <(E-Mail Removed)> 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
 
Reply With Quote
 
vova777@gmail.com
Guest
Posts: n/a
 
      07-10-2008
On Jul 9, 6:08*pm, Victor Bazarov <(E-Mail Removed)> wrote:
> * * *? (CHAR_BIT comes from <limits.h>)


Good point, thanks!

Regards,
Vladimir
 
Reply With Quote
 
Lionel B
Guest
Posts: n/a
 
      07-10-2008
On Wed, 09 Jul 2008 10:02:25 -0700, vova777 wrote:

> On Jul 9, 9:29*am, rudra <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bitset<32> and bitset<64> efficiency Ninds C++ 14 12-03-2012 11:02 PM
Is 'java.util.BitSet' available at JDK1.3? Joshua Java 4 05-06-2005 11:54 AM
Performance of java.util.BitSet Timo Nentwig Java 4 12-11-2003 12:43 AM
Re: bitset to UCHAR Victor Bazarov C++ 1 07-08-2003 09:11 PM
std::bitset construction with std::string question Dill Hole C++ 2 07-04-2003 11:48 PM



Advertisments