From 1480156ba08260d5837f0263df939b6ec63e2321 Mon Sep 17 00:00:00 2001 From: KenwoodFox Date: Thu, 10 Jun 2021 18:52:27 -0400 Subject: [PATCH] Create checkbit.cpp --- checkbit.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 checkbit.cpp diff --git a/checkbit.cpp b/checkbit.cpp new file mode 100644 index 0000000..6d25315 --- /dev/null +++ b/checkbit.cpp @@ -0,0 +1,40 @@ +#include +#include +#include + +/** + * Written by joe after feeling embarrased about the usage of & to find if + * a bit 'n' was 1 or 0. + **/ + +int main(int argc, char *argv[]) +{ + if(argc<3) // If the number of args is less than 3 then there were not enough expected args. + { + printf("\nPlease enter both an int and a [0-7] value to check that bit."); + exit(1); // Crash! + } + + int BINARY_NUM = atoi(argv[1]); // Store that binary number (use ascci to int) + int BIT = atoi(argv[2]); // What bit to check + + if(BINARY_NUM>254 || BIT>7) + { + printf("\nOne of your numbers was not formatted correctly, or out of range."); + exit(1); // Crash! + } + + // Check the bit using << + // Compares the number binary num using &, 1<