In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use Python bit operation". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to use Python bit operation"!
1. Judge whether the number is odd or even
Usually the way to judge whether an odd number is an even number is to divide it by 2 to see if the remainder is 0.
The Python code is as follows:
Def isodd (x): return True if (x% 20) else False
How to use bit operations?
We just need to use the & operation and do & with 1. If it is 1, the number is odd; if it is 0, then the number is even, and the Python code is as follows:
Def isodd (x): return True if (x & 1) else False
2. Moving one bit to the left is equal to multiplying 2, and moving one bit to the right is equivalent to dividing by 2.
One of the common problems encountered in the interview process is to write a binary search code.
The code for binary search is as follows:
Def binary_search (list, item):'': param list: ordered list: param item: element to find: return: item's index in list, if not returned None 'low = 0 high = len (list)-1 while low item: high = midpoint-1 return None in list
One of the steps is to take the intermediate value between the minimum subscript and the maximum subscript. If you use the bit operator, midpoint = (low + high) > > 1, the interviewer will be impressed.
3. Exchange two values
I'm sure you're all familiar with the code for numerical exchange, because it seems to have been used since the beginning of learning a programming language:
Temp = bb = aa = temp
But how do you use bit operations to do this?
A ^ = bb ^ = aa ^ = b
It is really difficult to understand, what is the principle?
The first line, a = a ^ b, is easy to understand
The second line, b = b ^ a = b ^ a ^ b, because b ^ b = 0, b = a ^ 0, that is, b = a
The third line, a = a ^ b, completes the value exchange because an is reassigned in the first step, so a = a ^ b ^ a = b.
Here, the characteristics of the XOR operation are summarized: the arbitrary number and its own XOR result is 0 and the XOR result of any number is still itself.
4. Find the uniqueness in the data list
There is a list of data (2N+1 integers), only one number appears once, and the other N numbers appear twice. How to find this unique data?
When you see this topic, I believe the algorithm that comes to mind for the first time must be counting, creating a list, looping through the entire data and counting, and then traversing the list to find data with a number of occurrences of 1.
Thus, the space complexity is O (N).
How to reduce the space complexity?
Take a look at the characteristics of XOR just mentioned: any number and its own XOR result is 0 XOR and any number XOR result is itself.
In that case, the result of N XOR that occurs twice is 0, and then the result of N XOR with the number of occurrence once is the number. That is, the way to find this unique data is to reduce the space complexity to O (1) by XOR operation on all the data.
5. Calculate how many 1s are in the binary number of a number
I believe that with the previous foundation, it is easy to implement this algorithm. Simply through the bit operation, and operation with 1 to see if the result is 1, and then move 1 bit to the right to continue to judge. The Python code is implemented as follows:
Def number1Bit (x): count = 0 while x: count = count + (xan1) x = x > > 1 return count
There is a problem with this, that is, if there are consecutive zeros, then multiple shifts need to be done. Is there an easy way to skip consecutive zeros?
That is to do the & operation with (xMel 1). It may not be easy to understand here. Give me an example.
X 1110 0000x-1 1101 1111x & (x Mel 1) 1100 0000
In this way, the last one will be detected.
The Python code is implemented as follows:
Def number1Bit (x): count = 0 while x: count = count + 1 x = x & (x Mel 1) return count so far, I believe you have a deeper understanding of "how to use Python bit operations". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.