Skip to content
Related Articles

Related Articles

C++ Program For Boolean to String Conversion

View Discussion
Improve Article
Save Article
  • Last Updated : 04 Jul, 2022

Here we will see how to convert boolean to string using a C++ program. Below are some examples:

Input: 1
Output: True

Input: 0
Output: False

In boolean algebra, there are only two values 0 and 1 which represent False and True. Thus, boolean to string conversion can be stated as:

Boolean  -> String

   1           -> True

   0           -> False

There are 2 ways to convert boolean to string in C++:

  1. Defining Customized Boolean To String Conversion Function.
  2. Using Alphanumeric Boolean Values.

Let’s start discussing each of these methods in detail.

1. Defining Customized Boolean To String Conversion Function

Example 1: Conversion of Truth Table for OR operation

Below is the C++ program to convert the truth table for OR operation:

C++




// C++ program to convert 
// truth table for OR ooperation
#include <iostream>
using namespace std;
  
// Function to convert boolean 
// into string
string btos(bool x)
{
  if(x) return "True";
  return "False";
}
  
// Driver code
int main() 
{
  // Conversion of Truth Table 
  // for OR operation
  cout << 1 << " OR " << 1 << 
          " is " << (1 | 1) << endl;
  cout << 1 << " OR " << 0 <<
          " is " << (1 | 0) << endl;
  cout << 0 << " OR " << 1 <<
          " is " << (0 | 1) << endl;
  cout << 0 << " OR " << 0 << 
          " is " << (0 | 0) << endl;
  cout << endl;
  cout << btos(1) << " OR " << btos(1) << 
          " is " << btos(1 | 1) << endl;
  cout << btos(1) << " OR " << btos(0) << 
          " is " << btos(1 | 0) << endl;
  cout << btos(0) << " OR " << btos(1) << 
          " is " << btos(0 | 1) << endl;
  cout << btos(0) << " OR " << btos(0) << 
          " is " << btos(0 | 0) << endl;
  return 0;
}

Output

1 OR 1 is 1
1 OR 0 is 1
0 OR 1 is 1
0 OR 0 is 0

True OR True is True
True OR False is True
False OR True is True
False OR False is False

Example 2: Conversion of Truth Table for AND operation

Below is the C++ program to convert the truth table for AND operation:

C++




//C++ program to convert the 
// truth table for AND operation
#include <iostream>
using namespace std;
  
// Function to convert boolean 
// into string
string btos(bool x)
{
  if(x) return "True";
  return "False";
}
  
// Driver code
int main() 
{
  // Conversion of Truth Table 
  // for AND operation
  cout << 1 << " AND " << 1 <<
          " is " << (1 & 1) << endl;
  cout << 1 << " AND " << 0 <<
          " is " << (1 & 0) << endl;
  cout << 0 << " AND " << 1 << 
          " is " << (0 & 1) << endl;
  cout << 0 << " AND " << 0 << 
          " is " << (0 & 0) << endl;
  cout << endl;
  cout << btos(1) << " AND " << btos(1) <<
          " is " << btos(1 & 1) << endl;
  cout << btos(1) << " AND " << btos(0) <<
          " is " << btos(1 & 0) << endl;
  cout << btos(0) << " AND " << btos(1) << 
          " is " << btos(0 & 1) << endl;
  cout << btos(0) << " AND " << btos(0) << 
          " is " << btos(0 & 0) << endl;
  return 0;
}

Output

1 AND 1 is 1
1 AND 0 is 0
0 AND 1 is 0
0 AND 0 is 0

True AND True is True
True AND False is False
False AND True is False
False AND False is False

2. Using Alphanumeric Boolean Values

Boolalpha flag indicates whether to use textual representation for bool values viz true or false or to use integral values for bool values viz 1 or 0. When the boolalpha flag is set then textual representation is used and when it is not set integral representation is used. By default, it is not set.

Below is the C++ program to convert boolean to string using boolalpha flag:

C++




// C++ program to convert boolean 
// to string using boolalpha flag
#include <iostream>
using namespace std;
  
// Driver code
int main()
{
    bool value = true;
  
    cout << "By default bool value is represented as " << 
             value << endl;
    cout << "Using boolalpha bool value is " << boolalpha << 
             value << endl;
  
    return 0;
}

Output

By default bool value is represented as 1
Using boolalpha bool value is true

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!