Header file
/*** function: Output shift operator Operation * Time: August 12, 2014 20:01:32* Author: cutter_point*/#ifndef printbinary_h_included#define Printbinary_h_ Included#include<iostream>using namespace Std;void printbinary (const unsigned char val) {for (int i=7; I! =-1; -i) { if (Val & (1<<i)) //bitwise operator, with cout<< "1"; Bar 1 Left I bit, if and Val is matched then the output 1, otherwise is 0 else //is a total of 8 bits a byte cout<< "0"; } #endif//printbinary_h_included
CPP file
/*** function: Output shift operator Operation * Time: August 12, 2014 20:01:43* Author: cutter_point*/#include "printBinary.h" #include <iostream># include<stdlib.h>using namespace std; #define PR (STR, EXPR) cout<<str; Printbinary (EXPR); Cout<<endl;int Main () {unsigned int getval; unsigned char a, b; cout<< "Enter a number from 0 to 255:"; Since char is a byte length of 8 bits, it is 0 to 255 cin>>getval; A=getval; PR ("A In binary:", a); cout<< "Enter a number from 0 to 255:"; cin>>getval; B=getval; PR ("B in binary:", b); cout<< "----------------------------------------------------------------------------" <<endl; PR ("A & B:", a&b); PR ("a | B: ", a|b); PR ("a ^ B:", a^b); PR ("~a", ~a); PR ("~b", ~b); cout<< "----------------------------------------------------------------------------" <<endl; unsigned char c=0x5a; PR ("C in binary:", c); a&=c; PR ("a&=c; A= ", a); A|=c; PR ("A|=C; A= ", a); A^=c; PR ("A^=C; A= ", a); a&=b; PR ("A&=b; A= ", a); A|=b; PR ("A|=B; A= ", a); A^=b; PR ("A^=B; A= ", a); b&=c; PR ("b&=c; B= ", b); B|=c; PR ("B|=C; B= ", b); B^=c; PR ("B^=C; B= ", b); System ("pause"); return 0;}