473,320 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C quivalent of std::bitset

In C++ this code:

for(int i=0; i<16; ++i){
std::cout << std::bitset<4>(i) << std::endl;

Will print numbers 1-15 as binaries with 4 bits.

Is there any equivalent of bitset in C?

Nov 15 '05 #1
3 7654

"Gaijinco" <ga******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
In C++ this code:

for(int i=0; i<16; ++i){
std::cout << std::bitset<4>(i) << std::endl;

Will print numbers 1-15 as binaries with 4 bits.

Is there any equivalent of bitset in C?


No, but it's not very difficult to write it yourself.

-Mike
Nov 15 '05 #2
Mike Wahler wrote:

"Gaijinco" <ga******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
In C++ this code:

for(int i=0; i<16; ++i){
std::cout << std::bitset<4>(i) << std::endl;

Will print numbers 1-15 as binaries with 4 bits.

Is there any equivalent of bitset in C?


No, but it's not very difficult to write it yourself.


/* BEGIN new.c */

#include <stdio.h>
#include <limits.h>
/**/
#define E_TYPE unsigned char
#define P_TYPE unsigned
#define STRING "%2u = 0x%x = %s\n"
#define INITIAL 0
#define FINAL 0xf
#define OFFSET (sizeof(e_type) * CHAR_BIT - 4)
/*/
#define E_TYPE unsigned
#define P_TYPE unsigned
#define STRING "%3u = 0x%2x = %s\n"
#define INITIAL 0
#define FINAL 0xff
#define OFFSET (sizeof(e_type) * CHAR_BIT - 8)
/**/
#define INC(E) (++(E))

typedef E_TYPE e_type;
typedef P_TYPE p_type;

void bitstr(char *str, const void *obj, size_t n);

int main(void)
{
e_type e;
char ebits[CHAR_BIT * sizeof e + 1];

puts("\n/* BEGIN output from new.c */\n");
for (e = INITIAL; FINAL >= e; INC(e)) {
bitstr(ebits, &e, sizeof e);
printf(STRING, (p_type)e, (p_type)e, OFFSET + ebits);
}
puts("\n/* END output from new.c */");
return 0;
}

void bitstr(char *str, const void *obj, size_t n)
{
unsigned mask;
const unsigned char *byte = obj;

while (n-- != 0) {
mask = ((unsigned char)-1 >> 1) + 1;
do {
*str++ = (char)(mask & byte[n] ? '1' : '0');
mask >>= 1;
} while (mask != 0);
}
*str = '\0';
}

/* END new.c */

--
pete
Nov 15 '05 #3
> No, but it's not very difficult to write it yourself.

I wrote this:

int change_base(int number, int base){
int pow=1;
int result=0;
while(n>0){
result+= n%base*pow;
pow*=10;
n/=base;
}
return result;
}

Which what really does is to write an decimal integer that looks like
binary, ternary, etc.

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dill Hole | last post by:
Can anyone tell me why std::bitset<2> foo(std::string("01")) initializes the bitset in reverse order, i.e. foo=true foo=false I would expect the bitset to be initialized in string text...
5
by: SpOiLeR | last post by:
Hi. q1: Is std::bitset<N> part of standard or it's compiler extension? q2: Is std::bitset::to_string() part of standard? q3: My documentation say this about std::bitset::to_string(): ...
14
by: Haro Panosyan | last post by:
How to construct a bitset from string? For example: std::bitset<16> b16("1011011110001011"); Is this possible? Thanks in advance. -haro
7
by: felixnielsen | last post by:
I would wery much like this to work: @code start #include <iostream> #include <bitset> const unsigned short size = 2; // 2^<unsigned int> // union V { char c; std::bitset<size*size*size> b;...
5
by: Sean Farrow | last post by:
Hi: I have an iterator defined as follows: std::map<std::bitset<6>, int>::iterator DotsIterator, SignsIterator; I get errors that std::bitset does not declare the < operator. Does this mean I...
3
by: Guy.Tristram | last post by:
Is there any good reason operator< is not defined for std::bitset? It seems to me that: 1 - it would be useful. 2 - it is easy to implement inside the class template. 3 - it is impossible to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.