Recently, the bit operations are frequently encountered, and the common bit operations are summarized as follows:
1. Displacement operator
>>: Called Bitwise right shift operator, moves the binary of the left number to the right by the specified number of digits.
The displacement operator acts on the variable on its left, and the value of the expression on the right side is the number of bits moved, and the result of the operation is the variable res
Most languages provide bitwise operators, which are widely used in languages such as c,c++, while there are not many application examples in scripting languages such as Js,as, and the appropriate use of bitwise operators can be a good result.Below according to own cognition simple talk about the bit operation use in JS (same applies to other languages), if have mistake, welcome correct correction.
One, bitwise operatorsIn general applications, we basically use an operator that is not in place. Although, it is based on the underlying, performance and speed will be very good, and that is because the lower level, the use of the difficulty is also very large, the underlying operation is converted to binary operation.There are seven bitwise operators, namely, bit non-not (~), Bit and and (), bit or OR (|)
1) PHP ~ Bitwise operators
PHP: bitwise operator-http://www.php.net/manual/zh/language.operators.bitwise.php
Some time ago, a foreigner posted an interesting things on twitter. after a string of suspected garbled characters was accessed, it could normally output 1337, so he had the following research results.
~ $ A Not (bitwise inversion) sets the bit 0 in $ a
This is a separate piece, because it is unclear (although the content is not enough), and I have not made it clear before, so it is necessary to write it out.
The bitwise operators also start from the difference between (| and | similar. In fact, for the logic and (and) operations of the two boolean values, the results are exactly the same as those of , but the performance of is better, because has the short circuit function. That is, A B calcul
and Operations
Example:35Solution: 3 of the twos complement is 11, 5 is 101, 35 is 011101, first look at the hundred (actually not the hundred, so it is easy to understand) a 1 1, according to (11=1,10=0,00=0,0 1=0) that the hundred should be 1, the same as the number 10=0, the number on the single digit 11=1, so the final result is 1. (It should have been a step later, because the value we get now is just the complement of the answer, but because the positive complement is itself, it's omitt
Yesterday in Leetcode do the problem when the 371, the original question is this:371. Sum of Integerscalculate the sum of of the integers a and B, but is not allowed to use the operator + and-. Example:given A = 1 and b = 2, return 3.Because I didn't use the bit arithmetic in practice at all, I first reacted when I saw this topic.1. Use multiplication instead of add and subtract, but a think, I'm afraid not, because multiplication is also added and subtracted in essence, it is impossible to skip
The following small series will bring you a PHP method that uses bitwise operations to manage website permissions. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at the following small series to bring you a PHP clever use of bit operations to achieve website permission management methods. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at it with
PHP bitwise and or (^, ) today, a friend in the group of friends asked the question of bitwise and OR ..
I have been working in PHP for a year. maybe I am not familiar with this part. here I will introduce this part to new users.
Operations on binary data are mainly performed by location.
This is not a simple addition.
Convert decimal 1 to binary 00000001
Convert decimal 2 to binary 0000001
Analysis of leetcode notes in Bitwise AND of Numbers Range
I. Description
Given a range[m, n]Where0 , Return the bitwise AND of all numbers in this range, inclusive.
For example, given the range[5, 7], You shoshould return4.
Ii. Question Analysis
Given a range[m, n], Where0 Returns the bitwise AND of All integers in the range, including the boundary.mAndn. For ex
Bitwise anti-operator in Javascript (~)
Bitwise anti-operator in Javascript (~) To perform a bitwise non (evaluate not) operation on an expression. For example ~ 1 =-2 ;~ 2 =-3 ;~ 99 =-100
The Code is as follows:
Result = ~ [Number]
All unary operators (such ~ Operators) are calculated according to the following rules:
The Code is as follows:
1. If it is appl
NOTE: Bitwise operators operate on the complement of integers, so the result of the operation is also complementary (with Operations)Convert data to complement form, then treat 0 as False, 1 as true, bitwise AND operation, and finally convert the result to decimal to display7 0000 0111 11 0000 1011————————————3 0000 0011Note:Even any number = evenPositive any number = positiveOdd 1 = 1Even 1 = 0| (or Ar
In the Java bit Operation Summary-leetcode Topic blog post summarizes the Java provides the bitwise operation operator, today also encounters the Leetcode the bitwise Operation TOPICGiven a range [M, n] where 0 For example, given the range [5, 7], and you should return 4.Test instructionsGiven M and N, all the numbers between M and N are returned with the result (including M and N)Problem Solving Ideas:The
complement to the original code of the complement of the original code with the same negative number of the original code for the value of the complement is added 1, such as-13 of the complement of 11110011, the original code is 10001101
-bit negation
Operation symbol ' ~ ', the binary of the number is reversed, the following assumes that all numbers in the computer using a byte representation
Example:
-13, binary representation is-1101, the complement is represented as 11110011
13 negation to
The Javascript bitwise-and assignment operator (=) Sets the result of a bitwise AND operation on the value of a variable and an expression value. Variables and expressions are treated as 32-bit binary values, and the general expressions are all decimal integers, which need to be converted to the corresponding binary, and then 0 forward to complement 32 bits.
Copy Code code as follows:
Resul
Is
6. Equivalent
24÷2÷2 = 6.
The displacement operation is the system bottom operation, the computation speed is much faster than multiplication and division, the optimized code optimizes to the extreme time, may convert the number into two common multiple, then carries on the displacement computation. However, there is little need in web development, and more than the bottom of the system, the program will be less portable.
The other bitwise ope
The use of bitwise operations to achieve addition is the computer with the binary operation, 32-bit CPU can only represent the number of 32 bits, where the first 1-digit addition to proceed, without regard to carry, based on the following 1 + 1 = 0 1 + 0 = 1 0 + 1 = 1 0 + 0 = 0
It is obvious that these expressions can be replaced by the "^" of the bitwise operation, as follows 1 ^ 1 = 0 1 ^ 0 = 1 0 ^ 1 = 1
1 #defineGpfcon (* (volatile unsigned long *) 0x56000050)//(int *) is to cast the variable to the address form so that it can be used with the pointer. 2 #defineGpfdat (* (volatile unsigned long *) 0x56000054)3 4 #defineGpgcon (* (volatile unsigned long *) 0x56000060)5 #defineGpgdat (* (volatile unsigned long *) 0x56000064)6 7 /*8 * led1,led2,led4 corresponds to GPF4, GPF5, GPF69 */Ten #defineGpf4_out (1 One #defineGpf5_out (1 A #defineGpf6_out (1 - - #defineGpf4_msk (3 the #defineGpf5_msk (3
when there are two operators, there are two operators, two operators, one operator, three operators BR> bit logic and | bit logic or ^ bit logic exclusive or -bit logic inverse right shift
bitwise operations detect, set, or shift the actual bit in a byte or word, it is only applicable to balanced and integer variables and their variants, and not to other data types.
note that bit operations and logical operations are distinguished.
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.