当前位置:网站首页>Bit operation of code
Bit operation of code
2022-07-21 17:50:00 【Invincible rookie ya】
java Middle displacement operation “<<“,“>>“,“>>>“
(1)"<<" For left shift operation
Whether positive or negative , Low complement 0;
(2) “>>” Shift right
If it's a positive number , Then the high position will be supplemented 0;
If it's negative , Then the high position will be supplemented 1;
(3) “>>>” For unsigned right shift operation
Whether positive or negative , All high places are filled 0;
Binary 1 The number of
public class Solution {
public int NumberOf1(int n) {
int res=0;
for(int i=0;i<32;++i){ // Attention bit operation & |
if((n & 1<<i)!=0) // Notice here is 1<<i, Move left i position
++res;
}
return res;
}
}
// characteristic n&(n−1), Will n The lowest bit in the binary of is determined by 1 become 0
public class Solution {
public int NumberOf1(int n) {
int res=0;
while (n!=0){
n&=(n-1);
++res;
}
return res;
}
}
Power operation
// Direct calculation
public class Solution {
public double Power(double base, int exponent) {
if(exponent<0){
base=1/base;
exponent=-exponent;
}
double res=1.0;
for(int i=0;i<exponent;++i)
res*=base;
return res;
}
}
public class Solution {
public double Power(double base, int exponent) {
if(exponent<0){
base=1/base;
exponent=-exponent;
}
return mypower(base,exponent);
}
public double mypower(double x,int y){
double res = 1;
while(y != 0){ // This is similar to divide and conquer
if((y & 1) != 0)
res *= x;
x *= x;
y = y >> 1;
}
return res;
}
}
String to number ( Digital output in [2^-32,2^32-1] Between )
import java.util.*;
public class Solution {
public static int StrToInt (String s) {
if(s==null && s.length()==0) return 0;
String string=s.trim();
if(string.length()==0) return 0;
boolean flag=true;
char numone=string.charAt(0);
long res=0; // Notice that this is Long Ah ah ah
if(!num(numone) && numone!='-' && numone!='+') return 0;
if(numone=='-') flag=false;
else if(numone=='+') flag=true;
else res=numone-'0';
for(int i=1;i<string.length();++i){
char c=string.charAt(i);
if(!num(c))
break;
else{
res=res*10+(c-'0'); // Otherwise, it will calculate according to overflow , The latter judgment doesn't work at all
if(flag && res>Integer.MAX_VALUE) return Integer.MAX_VALUE;
if(!flag && res*(-1)<Integer.MIN_VALUE) return Integer.MIN_VALUE;
}
}
if(flag) return (int) res;
else return (int) -res;
}
public static boolean num(char c){
if(c>='0' && c<='9')
return true;
else return false;
}
}
边栏推荐
猜你喜欢
随机推荐
Lesson 2 WiFi experiment of hi3861 station mode
How to extract coordinate information in MATLAB visual image window figure
如何用Sketch设计网页,创建栅格辅助线教程
[CS231N]Notes_1-Introduction
Ora-01461: only the long value to be inserted into the long column can be bound
数学建模 - K-means聚类
The latest method: set the href of a tag in electron development, jump to the default browser, and open the link
成为比开发更硬气的测试人,我都经历了什么?
C语言编译
Thread synchronization and interlock of C (42)
夏日大作战!卧兔网络带你看出海达人营销创意!
Encapsulate the global input component and bind parent-child data through V-model
C#(四十五)之线程池
C#(四十一)之线程
为啥有些经理,总是强行刷存在感?(你遇到过吗?)
ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值
openresty ngx.ctx请求上下文
Rsync combined with inotify to realize real-time file synchronization (2)
解决Metasploit中shell乱码的问题
Introduction and understanding of [robots protocol]