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<