Untitled

mail@pastecode.io avatar
unknown
plain_text
10 days ago
3.3 kB
1
Indexable
Never
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#define HEADER "Bit Handling Operations\n"
#define INPUT_PROMPT "Enter a positive integer (0 to 255): "
#define ERROR_EXIT "Not numeric or out of range!\n"
#define ERROR_INVALID "Invalid selection!\n"

// Function prototypes
void displayBinary(unsigned char n);
void performOperation(unsigned char *value, char operation);
void displayMenu();

int main() {
    unsigned char value;
    char operation;
    char input[10];  // Buffer to hold input
    
    printf(HEADER);
    printf(INPUT_PROMPT);
    
    // Read input and validate
    if (fgets(input, sizeof(input), stdin) != NULL) {
        // Remove the newline character if present
        input[strcspn(input, "\n")] = '\0';

        // Check if the input is numeric and within range
        char *endptr;
        long int num = strtol(input, &endptr, 10);

        // Check if strtol encountered non-numeric characters or if the value is out of range
        if (*endptr != '\0' || num < 0 || num > 255) {
            printf(ERROR_EXIT);
            return 1; // Exit if the input is invalid
        }

        value = (unsigned char)num; // Store the valid value
    } else {
        printf(ERROR_EXIT);
        return 1; // Exit if input reading fails
    }

    do {
        displayMenu();  // Display the menu

        // Read user input for operation
        if (fgets(input, sizeof(input), stdin) != NULL) {
            // Check for valid single character input
            if (input[1] != '\n' || !isalpha(input[0])) {
                printf(ERROR_INVALID); // Print error for invalid input
                continue;
            }

            operation = input[0];

            if (operation != 'E') {
                performOperation(&value, operation);  // Perform the selected operation
            }
        } else {
            break; // Exit if no input is provided
        }

    } while (operation != 'E'); // Continue until 'E' is entered

    return 0;
}

// Function to display the menu
void displayMenu() {
    printf("\n--------------------------\n");
    printf("Operations\n");
    printf("--------------------------\n");
    printf("L: Left-shift\n");
    printf("R: Right-shift\n");
    printf("C: Complement\n");
    printf("E: End program\n");
    printf("--------------------------\n");
    printf("Please select operation: ");
}

// Function to display binary representation
void displayBinary(unsigned char n) {
    for (int i = 7; i >= 0; i--) {
        printf("%d", (n >> i) & 1);
    }
    printf("\n");
}

// Function to perform the bit operation
void performOperation(unsigned char *value, char operation) {
    unsigned char result;

    switch (operation) {
        case 'L': // Left shift
            result = *value << 1;
            break;
        case 'R': // Right shift
            result = *value >> 1;
            break;
        case 'C': // Complement
            result = ~(*value);
            break;
        default:
            printf(ERROR_INVALID);
            return;
    }

    // Update the value with the result of the operation
    *value = result;

    // Display results
    printf("Decimal: %u, Hexadecimal: %2X, Binary: ", result, result);
    displayBinary(result);
}
Leave a Comment