Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

preface

Bitwise operations are used for low-level operations on numbers, that is, the bits (bits) that represent the data in memory. All values in ECMAScript are stored in IEEE 754 64-bit format, but bitwise operations do not apply directly to 64-bit representations. Instead, values are converted to 32-bit integers, then bitwise operations are performed, and then the result is converted to 64-bit. The author recently saw bit operation in LeetCode, and this article will put bit operation in some algorithm problems

LeetCode704: >>> binary lookup

Given an ordered (ascending) integer array nums with n elements and a target value, write a function to search for target in NUMs, returning a subscript if the target value exists, and -1 otherwise.

Example 1:

Input: nums = [-1,0,3,5,9,12], target = 9 Output: 4 Interpretation: 9 appears in nums with subscript 4

Example 2:

Input: nums = [-1,0,3,5,9,12], target = 2 Output: -1 Description: 2 does not exist in NUMS, so -1 is returned

Code section

We can use (left + right) >>> 1 instead of math.floor ((begin + end) / 2) for mid.

LeetCode165: ~~ Compare version number

I give you two version numbers version1 and version2, please compare them. A version number consists of one or more revision numbers connected by a ‘.’. Each revision number consists of multiple digits that may contain leading zeros. Each version number contains at least one character. Revision numbers are numbered from left to right, with subscripts 0, the leftmost revision number subscript 0, the next revision number subscript 1, and so on. For example, 2.5.33 and 0.1 are valid version numbers. When comparing version numbers, compare their revision numbers from left to right. When comparing revision numbers, only the integer values after ignoring any leading zeros are compared. That is, revision number 1 is equal to revision number 001. If the version number does not specify a revision number at a subscript, the revision number is treated as 0. For example, version 1.0 is less than version 1.1 because they have the same revision number with subscript 0, and the revision number with subscript 1 is 0 and 1, respectively, with 0 < 1. The return rule is as follows: 1 is returned if version1 > version2, -1 is returned if version1 < version2, and 0 is returned otherwise.

Example 4:

Input: version1 = “1.0.1”, version2 = “1” Output: 1

Analysis of ideas:

  • The version numbers v1 and v2 are divided into arrays according to the symbol “.”, and the sizes are compared from left to right.
  • V1 is greater than v2 and returns 1, v2 is less than v2 and returns -1, v1 is equal to v2 and returns 0.

Code section

In order to compare the numbers, we can use the bit operation to double non round, ~~ will completely ignore the decimal part, this method is obviously more concise than parseInt, so it is better to write double non

The last

⚽ this article mainly introduces the bit operation in solving algorithm problems of some SAO operation ~ ⚾ if you are interested in this article welcome to like attention + collection, more wonderful knowledge is waiting for you! 😘 πŸ€GitHub blogs at github.com/Awu1227. πŸ‰ I have other columns, please read ~ 🏐 play the beauty of CSS 🎱Vue from giving up to getting started 🎳 simple JavaScript