bitset in C

R

rudra

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?
 
J

Juha Nieminen

rudra said:
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().)
 
V

vova777

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
 
L

Lionel B

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 ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top