Added to bitwise operators program

Added a setBit function that prompts the user for which bit to set to true or false, and an insertBitPattern function that adds a set of bits input by the user then inserts them into a specified position.

setBit:

int setBit(int val, int n, bool setToOne)
{
   int mask = setToOne ? 1 << (n-1) : ~(1 << (n-1));

   printf("\nMask created for %s", (setToOne) ? "true:  " : "false: ");
   bitrep(mask);
   printf("\n");

   mask = setToOne ? 1 << (n-1) : ~(1 << (n-1));

   if(setToOne)
   {
       printf("using | (OR)\n");
       val = val | mask;
   }
   else
   {
      printf("using & (AND)\n");
      val = val & mask;      
   }
   
   printf("After set, \n");   
   printf("Bit Representation is:  ");

   return val;
}

insertBitPattern:

void insertBitPattern(int val)
{    
    // forloop variables.
    int i;
    int j = 0;
    // nth bit, mask and user's bit pattern variables.
    int nth_bit;
    int mask;
    char bit[MAXHEX];
    
    printf("Enter bit pattern (set of 0's & 1's): ");
    scanf("%s", &bit);

    //printf("%s length is %d\n", bit, strlen(bit));

    printf("Enter nth position to enter bit pattern: ");
    scanf("%d", &nth_bit);

    // getmask for integer
    mask = 1 <= 0; i--, mask = mask << 1)
    {
        //printf("mask representation:    ");
        if (bit[i] == '1')
        {
            val = val | mask;
            //bitrep(mask);
        }
        else 
        {
            val = val & ~mask;
            //bitrep(~mask);
        }
       /*printf("\n");        
       printf("val representation is:  ");
       bitrep(val);
       printf("\n");*/
    }
    printf("val representation is:  ");
    bitrep(val);
    printf("\n");   
}

Bits and Binary operators program

This program is based off of the recent topics discussed in our OOP344 class about bits and binary operators.  I used the bits function made by our teacher last class.  This program asks the user for a hexadecimal integer and gives the option to left shift, right shift, reverse it, and also compare it with another hexadecimal integer, using the &, | and ^ operators.  It also displays the bits in sets of fours.

Since there is a difference with signed integers when it is right shifted, I put a signed version of the program that can be used by commenting out the unsigned #define version at the top of the program.

I posted it in a public repository on GitHub:

https://github.com/lkisac/My-Programs/blob/master/BinaryOperators/BinaryOperators/binaryoperators.cpp

Binary to Decimal conversion program

This is a weekly challenge for OOP344.  This program converts a set of binary digits input by the user and converts it to its decimal value.

#include 
#include 
using namespace std;
#define NUMOFDIGITS 30

int getBinary(int x)
{
	int i; // for loop integer
	int power = 1; // power of 2 to calculate with each digit in x
	int dec_result = 0; // decimal result

	string s = to_string(x); // converts int x to string s
	int x_size = s.length(); // gets size of x

	// stores x into integer array
	int xarray[NUMOFDIGITS];
	for (i = 0; i  NUMOFDIGITS ? x_size = NUMOFDIGITS : x_size = x_size; // to allow only maximum number(NUMOFDIGITS) of digits.

	// Calculates decimal value
	for (i = x_size-1; i >= 0; i--)
	{
		dec_result += xarray[i] * power; // adds to dec_result starting at 1 * xarray[i], 2 * xarray[i], 4, 8, 16 etc..
		power *= 2; // increments power to double of power each time.  1, 2, 4, 8, 16, 32 etc..
	}

	return dec_result;
}

int main ()
{
	int x;

	cout  x;
	fflush(stdin);

	cout << "Decimal value is: " << getBinary(x) << endl;

	getchar();
	return 0;
}