4

I'm somewhat new to C++, and I was wondering how to scanf into or printf out of a bitset, i.e., what is the appropriate type specifier for I/O to a bitset index? An example of what I would want to do is:

#include <bitset>
#include <stdio.h>

using namespace std; 

int main() 
{
    bitset<1> aoeu;
    scanf("%d" &bitset[0]); //this line
    printf("%d" bitset[0]); // this line
}
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
skang
  • 41
  • 3
  • 2
    Why? If you're using C++, then use C++ features (like `cin`/`cout`). You can use [to_string](https://en.cppreference.com/w/cpp/utility/bitset/to_string) then print out its char array. – ChrisMM Jul 24 '20 at 22:49
  • You cannot use `scanf` directly with bitsets, because `scanf` requires an address to a byte, not a bit. However, you can first use `scanf` to write to a byte (`unsigned char`) or an `int` and then convert it to bits for the bitset. – Andreas Wenzel Jul 24 '20 at 23:35

3 Answers3

3

As ChrisMM mentioned, you should use the the C++ way of doing input and output. Luckily, a std::bitset has overloads for operator<< and operator>> so you directly read from std::cin and write to std::cout, without needing to_string(), like so:

#include <bitset>
#include <iostream>
 
int main() 
{
    std::bitset<1> aoeu;
    std::cin >> aoeu; // read bitset from standard input
    std::cout << aoeu; // write bitset to standard output
}

If you just want to read one specific bit and put it in a bitset, you have to do it a bit more indirect:

std::bitset<3> bits;
int value;
std::cin >> value; // read one integer, assuming it will be 0 or 1
bits[1] = value; // store it in the second bit of the bitset
G. Sliepen
  • 7,637
  • 1
  • 15
  • 31
  • 1
    You did not answer the OP's main question on how to accomplish this using `scanf` and `printf`. Since the OP's question probably is an [XY problem](http://xyproblem.info/), you were correct to show the OP the correct way to solve the underlying problem, without using `scanf` and `printf`. Therefore, I have upvoted your answer. However, for completeness, I have written my own answer, in which I answer the OP's question on how it can be accomplished with `scanf` and `printf`. – Andreas Wenzel Jul 25 '20 at 00:13
3

Your question on how to accomplish this with scanf and printf seems to be an XY problem. The answer provided by @GSliepen shows you how to do it properly in C++.

However, in case you are really interested on how to accomplish this using scanf and printf (which I don't recommend), I will give you a direct answer to your question:

You cannot use scanf directly with bitsets, because scanf requires an address to at least a byte, not a single bit. Addresses to single bits don't even exist (at least on most platforms). However, you can first use scanf to write to a temporary byte (unsigned char) or an int and then convert it to a bit for the bitset, for example like this:

#include <bitset>
#include <cstdio>
#include <cassert>

int main() 
{
    std::bitset<1> aoeu;
    int ret, input;

    ret = std::scanf( "%d", &input );
    assert( ret == 1 );

    aoeu[0] = input;

    std::printf( "%d\n", static_cast<int>(aoeu[0]) );
}

In case you are wondering why I am not using namespace std;, although you do in your question, then you may want to read this StackOverflow question.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
0

To print the value of a specific bit j printf("%d", aoeu(j));

To print the value of the whole bitset I would suggest using

printf("%s\n", val.to_string().c_str());

Tahuti
  • 31
  • 2