Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   bitset in C (http://www.velocityreviews.com/forums/t624591-bitset-in-c.html)

rudra 07-09-2008 08:29 AM

bitset in C
 
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?

Juha Nieminen 07-09-2008 01:16 PM

Re: bitset in C
 
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().)

vova777@gmail.com 07-09-2008 05:02 PM

Re: bitset in C
 
On Jul 9, 9:29*am, rudra <bnrj.ru...@gmail.com> 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

vova777@gmail.com 07-10-2008 09:45 AM

Re: bitset in C
 
On Jul 9, 6:08*pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> * * *? (CHAR_BIT comes from <limits.h>)


Good point, thanks!

Regards,
Vladimir

Lionel B 07-10-2008 10:01 AM

Re: bitset in C
 
On Wed, 09 Jul 2008 10:02:25 -0700, vova777 wrote:

> On Jul 9, 9:29*am, rudra <bnrj.ru...@gmail.com> 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


All times are GMT. The time now is 10:23 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.