BITWISE CALCULATOR

BITWISE.H

/*######################################################################################
# Dev: cnd.dev
# Program Name: N/A
# Version: 1.0.1
#  - Major.Minor.Update/BuildNumber
# Date: 181445MAR25
# Filename: bitwise.h
# Dependency: N/A
# Compile Cmd: gcc -m64 -O1 bitwiseCalculator.c bitwise.h bitwise.c -o BitwiseCalculator-v1.0.0-linux-x86-64
# Synopsis:
#  - Overview: describes what the program does, how it works, and its key components
#  - Technical: ...
######################################################################################*/

#ifndef BITWISE_H
#define BITWISE_H

//SELECTION MODE
int selectMode(int* mode);

//GET DATA - AND/OR/XOR
int getData(int* userInput1, int* userInput2);

//PROCESS DATA
int binaryStringToInt(const char* binaryStr);

int bitwiseAND(int userInput1, int userInput2);

int bitwiseOR(int userInput1, int userInput2);

int bitwiseXOR(int userInput1, int userInput2);

int bitwiseNOT(int userInput1);

//OUTPUT
void printBinary(unsigned int result);

#endif

 * Include guards and #pragma once prevent a header file from being included multiple
   times during compilation, which avoids errors like multiple definitions or 
   redefinitions of functions and variables. 
    - if not specified, repeated inclusions (especially through nested headers) can
      lead to compiler errors.
    - the pragma once is not officially part of the C standard

BITWISE.C

/*######################################################################################
# Dev: cnd.dev
# Program Name: N/A
# Version: 1.0.1
#  - Major.Minor.Update/BuildNumber
# Date: 181445MAR25
# Filename: bitwise.c
# Dependency: bitwise.h
# Compile Cmd: gcc -m64 -O1 bitwiseCalculator.c bitwise.h bitwise.c -o BitwiseCalculator-v1.0.0-linux-x86-64
# Synopsis:
#  - Overview: describes what the program does, how it works, and its key components
#  - Technical: ...
######################################################################################*/

#include <stdio.h>
#include "bitwise.h"

//INPUT
int selectMode(int* mode){
  while (printf("mode: "), scanf("%d", mode) != 1){
    while (getchar() != '\n');
  }
  return 0;
}

int getData(int* userInput1, int* userInput2){
    char buffer1[33];
    char buffer2[33];

    while (printf("Enter first number (binary): \t"), scanf("%32s", buffer1) != 1)
        while (getchar() != '\n');

    while (printf("Enter second number (binary): \t"), scanf("%32s", buffer2) != 1)
        while (getchar() != '\n');

    *userInput1 = binaryStringToInt(buffer1);
    *userInput2 = binaryStringToInt(buffer2);

    return 0;
}

//PROCESS
int binaryStringToInt(const char* binaryStr) {
    int result = 0;
    
    while (*binaryStr == '0' || *binaryStr == '1') {
        result = result * 2 + (*binaryStr - '0');
        binaryStr++; 
    }
    
    return result;
}
int bitwiseAND(int userInput1, int userInput2){
  return userInput1 & userInput2;
}

int bitwiseOR(int userInput1, int userInput2){
  return userInput1 | userInput2;
}

int bitwiseXOR(int userInput1, int userInput2){
  return userInput1 ^ userInput2;
}

int bitwiseNOT(int userInput1){
  return ~userInput1;
}

//OUTPUT
void printBinary(unsigned int result){
  for (int i = 31; i >= 0; i--) {
    printf("%d", (result >> i) & 1);
      if (i % 8 == 0 && i != 0) {
        printf(" ");  
      }
  }
}

BITWISECALCULATOR.C

/*######################################################################################
# Dev: cnd.dev
# Program Name: BitwiseCalculator-v1.0.0-linux-x86-64
# Version: 1.0.1
#  - Major.Minor.Update
# Date: 181445MAR25
# Filename: bitwiseCalculator.c
# Dependency: N/A
# Compile Cmd: gcc -m64 -O1 bitwiseCalculator.c bitwise.h bitwise.c -o BitwiseCalculator-v1.0.0-linux-x86-64
# Synopsis:
#  - Overview: describes what the program does, how it works, and its key components
#  - Technical: ...
######################################################################################*/

#include <stdio.h>
#include "bitwise.h"

int main(int argc, char *argv[]) {

  int mode = 0;
  int userInput1 = 0;
  int userInput2 = 0;
  unsigned int andResult = 0;
  unsigned int orResult = 0;
  unsigned int xorResult = 0;
  unsigned int notResult = 0;
    
  puts("Select mode: [1=&], [2=|], [3=^], [4=~]");
  selectMode(&mode);
  
  switch (mode){
    case 1:{
            
      //input
      puts("\n*******AND MODE*******");
      getData(&userInput1, &userInput2);
            
      //process
      andResult = bitwiseAND(userInput1, userInput2);
      
      //output
      printBinary(andResult);
            
      break;
    }
       
    case 2:{
      
      //input
      puts("\n*******OR MODE*******");
      getData(&userInput1, &userInput2);
            
      //process
      orResult = bitwiseOR(userInput1, userInput2);
            
      //output
      printBinary(orResult);
    
      break;
    }
        
    case 3:{
      //input
      puts("\n*******XOR MODE*******");
      getData(&userInput1, &userInput2);
            
      //process
      xorResult = bitwiseXOR(userInput1, userInput2);

            
      //output
      printBinary(xorResult);
    
      break;
    }
        
    case 4:{
      //input
      puts("\n*******NOT MODE*******");
      while (printf("Enter a value to negate: "), scanf("%d", &userInput1) != 1)
        while (getchar() != '\n');
            
      //process
      notResult = bitwiseNOT(userInput1);
            
      //output
      printBinary(notResult);
    
      break;
    }
        
    default:{
      puts("Invalid selection...re-run the program again to continue");
    }
  }

  return 0;
}

Last updated