⚙️ C++ Source Code

Simple first-semester C++ programs — using namespace std with easy-to-understand code

Note: Browsers cannot execute native C++ code directly. These programs run on your computer with a standard C++ compiler (like g++ or MinGW). The interactive tools on the Tools page use JavaScript so you can test the exact same functionality inside your browser.
01

Text-to-Binary Converter

File: cpp/text_binary.cpp

C++
What it does: Reads text from the user, uses a simple for loop to examine each character, and prints its ASCII decimal value alongside its 8-bit binary representation.
Key C++ Concepts Used
using namespace std;

Allows using cout, cin, and string directly without typing std:: each time.

bitset<8>

From <bitset> library. Easily formats any number into an 8-bit binary string.

int ascii = ch;

Assigning a char to an int automatically gives its ASCII number (e.g. 'A' gives 65).

getline(cin, text);

Reads the whole user sentence including spaces (unlike standard cin >>).

📄 cpp/text_binary.cpp — Clean & Simple Source Compile: g++ -o text_binary text_binary.cpp
// Text-to-Binary Converter — cpp/text_binary.cpp
// Simple 1st-semester C++ implementation

#include <iostream>
#include <string>
#include <bitset>

using namespace std;

int main() {
    string text;

    cout << "=== Text-to-Binary Converter ===" << endl;
    cout << "Enter text: ";
    getline(cin, text);   // Reads full sentence with spaces

    cout << endl;
    cout << "Char\tASCII\tBinary" << endl;
    cout << "------------------------" << endl;

    // Simple for loop through each character
    for (int i = 0; i < text.length(); i++) {
        char ch = text[i];
        int ascii = ch;              // 'H' becomes 72
        bitset<8> binary(ch);        // Converts to 8-bit binary string

        cout << ch << "\t" << ascii << "\t" << binary << endl;
    }

    return 0;
}
Sample Output — Input: "Hi!"
=== Text-to-Binary Converter === Enter text: Hi! Char ASCII Binary ------------------------ H 72 01001000 i 105 01101001 ! 33 00100001
▶ Run this tool in the browser →
&

Bitwise Operations Calculator

File: cpp/bitwise.cpp

C++
What it does: Prompts the user for numbers and an operation choice (1–6). It calculates the result using C++ bitwise operators and prints values in both decimal and binary format using bitset<8>.
Bitwise Operators Reference
Operator C++ Symbol What it does Example (a=12, b=10)
AND & Result is 1 only if both bits are 1 12 & 10 = 8
OR | Result is 1 if either bit is 1 12 | 10 = 14
XOR ^ Result is 1 if bits are different 12 ^ 10 = 6
NOT ~ Inverts every bit (0 becomes 1, 1 becomes 0) ~12
Left Shift << Shifts bits left (multiplies by 2 each step) 12 << 1 = 24
Right Shift >> Shifts bits right (divides by 2 each step) 12 >> 1 = 6
📄 cpp/bitwise.cpp — Clean & Simple Source Compile: g++ -o bitwise bitwise.cpp
// Bitwise Operations Calculator — cpp/bitwise.cpp
// Simple 1st-semester C++ implementation

#include <iostream>
#include <bitset>

using namespace std;

int main() {
    int a, b, choice;

    cout << "=== Bitwise Operations Calculator ===" << endl;
    cout << "1. AND  (&)" << endl;
    cout << "2. OR   (|)" << endl;
    cout << "3. XOR  (^)" << endl;
    cout << "4. NOT  (~)" << endl;
    cout << "5. Left Shift  (<<)" << endl;
    cout << "6. Right Shift (>>)" << endl;

    cout << "\nEnter first number (a): ";
    cin >> a;

    cout << "Choose operation (1-6): ";
    cin >> choice;

    cout << "\na in binary: " << bitset<8>(a) << endl;

    if (choice == 1) { // AND
        cout << "Enter second number (b): ";
        cin >> b;
        cout << "b in binary: " << bitset<8>(b) << endl;

        int result = a & b;
        cout << "\nResult (a & b) = " << result << endl;
        cout << "Binary result  = " << bitset<8>(result) << endl;
    }
    else if (choice == 2) { // OR
        cout << "Enter second number (b): ";
        cin >> b;
        cout << "b in binary: " << bitset<8>(b) << endl;

        int result = a | b;
        cout << "\nResult (a | b) = " << result << endl;
        cout << "Binary result  = " << bitset<8>(result) << endl;
    }
    else if (choice == 3) { // XOR
        cout << "Enter second number (b): ";
        cin >> b;
        cout << "b in binary: " << bitset<8>(b) << endl;

        int result = a ^ b;
        cout << "\nResult (a ^ b) = " << result << endl;
        cout << "Binary result  = " << bitset<8>(result) << endl;
    }
    else if (choice == 4) { // NOT
        int result = ~a;
        cout << "\nResult (~a)   = " << result << endl;
        cout << "Binary result = " << bitset<8>(result) << endl;
    }
    else if (choice == 5) { // Left Shift
        int shift;
        cout << "Enter shift amount: ";
        cin >> shift;

        int result = a << shift;
        cout << "\nResult (a << " << shift << ") = " << result << endl;
        cout << "Binary result  = " << bitset<8>(result) << endl;
    }
    else if (choice == 6) { // Right Shift
        int shift;
        cout << "Enter shift amount: ";
        cin >> shift;

        int result = a >> shift;
        cout << "\nResult (a >> " << shift << ") = " << result << endl;
        cout << "Binary result  = " << bitset<8>(result) << endl;
    }
    else {
        cout << "Invalid choice!" << endl;
    }

    return 0;
}
Sample Output — a=12, b=10, operation=AND
=== Bitwise Operations Calculator === 1. AND (&) 2. OR (|) 3. XOR (^) 4. NOT (~) 5. Left Shift (<<) 6. Right Shift (>>) Enter first number (a): 12 Choose operation (1-6): 1 a in binary: 00001100 Enter second number (b): 10 b in binary: 00001010 Result (a & b) = 8 Binary result = 00001000
▶ Run this tool in the browser →
🖥️

How to Compile & Run Locally

Windows (MinGW / g++) or Online C++ Compilers

Using Command Prompt (Windows)
  1. Open Command Prompt and navigate to your project's cpp/ folder
  2. To compile Text to Binary: g++ -o text_binary text_binary.cpp
  3. To run it: ./text_binary
  4. To compile Bitwise Calculator: g++ -o bitwise bitwise.cpp
  5. To run it: ./bitwise
Running online (no installation required): You can copy-paste this simple code into onlinegdb.com or Programiz C++ Compiler to run it instantly in any web browser.