Java programming those things 25-bit Operators

Source: Internet
Author: User
Tags bitwise operators

Java programming those things 25-bit operators Zhengzhou game college Chen yuefeng from: http://blog.csdn.net/mailbomb 4.5 binary OperatorsBecause data in the computer is in binary format, Java provides binary operators for direct operations. This is the bitwise operators and shift operators to be explained below. Binary operators can be used to operate numbers directly on the basis of binary. The execution efficiency is much higher than that of ordinary mathematical operators. These operators are widely used in network programming, hardware programming, and other fields. Binary operators have limited mathematical meaning. In Java code, the default value for Direct Writing and output is decimal. in Java code, binary values cannot be directly written, but octal and hexadecimal numbers can be written. octal values start with digits 0, for example, 016 and hexadecimal values start with numbers 0 and X, for example, 0x12 and 0xaf. When calculating binary operations, the Java Execution Environment (JRE) first converts the decimal number to binary, and then performs operations. If the value of the output result is returned, the number is converted to hexadecimal format. Note: 1. The positive number of machines is the original code, and the negative number of machines is the complement code. Be careful when calculating. For details about the calculation of binary and complement code, see Java programming things 7-the concept of hexadecimal, and Java programming Things 8-The data expression inside the computer. 2. integer calculation results are all int type, regardless of whether bytes or short are binary. 4.5.1 bitwise operatorsThere are four major bitwise operators in Java: & (bitwise AND), | (bitwise OR), ^ (XOR), and ~ (Bitwise inversion). The following describes the operation rules and examples in sequence. L & (and) operation rule: the number involved in the operation, low-level alignment, Zero Filling for the low-level deficiency, the corresponding binary bit is 1, then the operation result is 1, otherwise it is 0. Applicable: block one or more digits. Because any number and 0 and 0 are both 0. Sample Code: int A = 4; int B = 10; int c = A & B; calculation process: 4 binary form 0000 0000 0000 0000 0000 0000 0000 0100 10 binary form 0000 0000 0000 0000 0000 0000 0000 1010 According to calculation rules, the result is 0000 0000 0000 0000 0000 0000 0000. If the number is converted to decimal, it is the number 0l. | (OR) operation rule: the number involved in the operation, the low-level alignment, and the low-level zero filling, if one of the corresponding binary bits is 1, it is 1; otherwise, it is 0. Applicable scenario: change one or more digits to 1. Because 1 and any number are either 1. Sample Code: int A = 4; int B =-10; int c = A | B; calculation process: 4 binary form 0000 0000 0000 0000 0000 0000 0000 0100-10 binary form 1111 1111 1111 1111 1111 1111 1111 0110 According to calculation rules, the result is 1111 1111 1111 1111 1111 1111 1111 0110. The binary number is converted to the decimal number-10. L ^ (XOR) operation rule: numbers involved in the operation, low-level alignment, zero completion of the upper-level deficiency, the corresponding binary phase is the same as zero, not the same as 1. Applicable scenario: Determine whether the digits correspond to the same bits. Sample Code: int A = 4; int B = 10; int c = a ^ B; calculation process: 4 binary form 0000 0000 0000 0000 0000 0000 0000 0100 10 binary form 0000 0000 0000 0000 0000 0000 0000 1010 According to calculation rules, the result is 0000 0000 0000 0000 0000 0000 0000. The decimal number is 14l ~ (Not) operation rule: operate only one number, and change the bitwise of 1 in this number to 0, and the bitwise of 0 to 1. Applicable scenario: Sample Code for reverse numeric content: int A = 4; int c = ~ A. calculation process: the binary format of 4 is 0000 0000 0000 0000 0000 0000 0000 0100 according to the calculation rules, the result is 1111 1111 1111 1111 1111 1111 1111. The decimal number is-5. In fact, bitwise operations should be consistent with the actual implementation, that is, the provided limit-level operator number, each operator has a corresponding circuit implementation. Simple Example: l converts any number to a positive number. If n is an arbitrary integer, the code for converting n to a positive number is int M = N & 0x7fffffff; l judge whether the value of the last third digit of any number is 1. If n is an arbitrary integer, the code is: int M = N & 0x4; boolean B = (M! = 0); L returns the fourth position of any number to 1. If n is an arbitrary integer, the code is: int M = n | 0x8;

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.