// bits.cpp 
// sample problem that shows how to set bits in char and int variables

#include <iostream>
using namespace std;

int main() {

	// recipe on how to put bits into a character
    // we will put 8 bits into character c 
	// we will put bits: 01100001, which corresponds to 
	// the ASCII code for character "a"

	unsigned char c; // it is very important to be unsigned 
	        // so that 11111111 is not understood as a negative number!!!

	int a[] = {0,1,1,0,0,0,0,1}; // initializing array with 8 integers 

	c = 0;

	for (int i=0; i<8; i++) {
	   c = c << 1; // shifts all bits of c one position to the left
	   c += a[i]; // adds number a[i], which now becomes the right most bit of c
	}

	cout<< "c = "<<c<<"\n";

	// recipe on how to put bits into integer n
	// will keep putting bits into integer n,
	// we will put bits 1101, which is number 13 
    

	unsigned int n;
	int b[] = {1,1,0,1}; // initializing arrays with 4 integers

	n = 0;

	for (i=0; i<4; i++) {
	   n = n << 1; // shifts all bits of n one position to the left
	   n += b[i]; // adds number b[i], which now becomes the right most bit of n
	}

	cout<< "n = "<<n<<"\n";

	// note: an elegant way of combining bits into a variable,
	// is to have an auxiliary method or function that pushes bits
	// into the variable; such a function receives the bit
	// and operates on the variable; it would contain the statements
	// appearing within the above loops.

	return 0;
}