"Thinkinginc++" 13, operation of output shift operator

Source: Internet
Author: User


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;}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.