Summary of C + + bitwise operators

Source: Internet
Author: User

1#include <stdio.h>2#include <memory.h>3#include <malloc.h>4 #defineMaxbinlength 165 6 //Get the binary of unsigned number, which I wrote myself, a simpler way to use Bitset instead7 Char* Getunsignedbinary (unsignedintnum)8 {9     intLen = maxbinlength-1;Ten     Char*bin = (Char*)malloc((Maxbinlength +1)*sizeof(Char)); Onememset (Bin,'0', maxbinlength); ABin[maxbinlength] =0; -      while(num/2!=0){ -bin[len--] ='0'+ num%2; theNum/=2; -     } -     if(num!=0) -bin[len--] ='1'; +     returnbin; - } +  A voidFlip (unsigned Shortnum) at { -printf"bitwise negation, operator is ~, function is simple, each bits of the operand is reversed \ n"); -printf"Original value:%6d%s\n", Num,getunsignedbinary (num)); -printf"new value:%6d%s\n", ~num,getunsignedbinary (~num)); -printf"-------------------------\ n"); - } in  - voidLeft_shift (unsigned Shortnum,unsigned Shortoffset) to { +printf"The left-shift operation, the operator is <<, moves the operand's bits to the left by the specified offset (%d) number, offset must be less than the number of digits of the original, and the newly added bit on the right is filled with 0 \ n", offset); -printf"Original value:%6d%s\n", Num,getunsignedbinary (num)); theprintf"new value:%6d%s\n", Num<<offset,getunsignedbinary (num<<offset)); *printf"-------------------------\ n"); $ }Panax Notoginseng  - voidRight_shift (unsigned Shortnum,unsigned Shortoffset) the { +printf"The right shift operation, the operator is >>, moves the bits of the operand to the right by the specified offset (%d) number, offset must be less than the number of digits of the original, \n\ A if the operand is unsigned, the newly added bit on the left is filled with 0, and if the operand is signed, the newly added bit on the right may be filled with a sign bit, or it may be filled with 0, \ n theDepending on the machine, see note \ n", offset); +printf"Original value:%6d%s\n", Num,getunsignedbinary (num)); -printf"new value:%6d%s\n", Num>>offset,getunsignedbinary (num>>offset)); $printf"-------------------------\ n"); $ } -  - voidWeiyu (unsigned Shortnum,unsigned Shortnum_1) the { -printf"bitwise AND, the operator is &, each corresponding bits of two operands is ' and ', and is operated in a manner similar to the && operation \ n");Wuyiprintf"Original value:%6d%s\n", Num,getunsignedbinary (num)); theprintf"Original value:%6d%s\n", Num_1,getunsignedbinary (num_1)); -printf"new value:%6d%s\n", Num&num_1,getunsignedbinary (num&num_1)); Wuprintf"-------------------------\ n"); - } About  $ voidWeihuo (unsigned Shortnum,unsigned Shortnum_1) - { -printf"The bitwise OR, operator is |, the ' or ' operation for each corresponding bits of two operands, in a manner similar to | | Operation \ n"); -printf"Original value:%6d%s\n", Num,getunsignedbinary (num)); Aprintf"Original value:%6d%s\n", Num_1,getunsignedbinary (num_1)); +printf"new value:%6d%s\n", Num|num_1,getunsignedbinary (num|num_1)); theprintf"-------------------------\ n"); - } $  the voidWeiyihuo (unsigned Shortnum,unsigned Shortnum_1) the { theprintf"bitwise XOR, the operator is ^, each corresponding bits of two operands is ' XOR ' operation, two bits are different 1, the same is 0\n"); theprintf"Original value:%6d%s\n", Num,getunsignedbinary (num)); -printf"Original value:%6d%s\n", Num_1,getunsignedbinary (num_1)); inprintf"new value:%6d%s\n", Num^num_1,getunsignedbinary (num^num_1)); theprintf"-------------------------\ n"); the } About  the voidZhushi () the { theprintf"NOTE: Bitwise operators can only manipulate integers, and the integer may be signed or unsigned, \n\ + if the operand is a negative number, then how the bit handles its sign bit depends on the machine, so implemented in an environment \ n - The program may not be available for another environment, so it is highly recommended to use the unsigned integer as the operand \ n theReference <<C++primer>> (People's post and Telecommunications Publishing House 4th edition) 155 page \ n");Bayiprintf"-------------------------\ n"); the } the  - intMain () - { theUnsigned Shortnum = the, num_1 = -, offset =3; the Flip (num); the Left_shift (num,offset); the Right_shift (num,offset); - Weiyu (num,num_1); the Weihuo (num,num_1); the Weiyihuo (num,num_1); the Zhushi ();94     return 0; the}
View Code

Summary of C + + bitwise operators

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.