当前位置:网站首页>Print bit information of numbers
Print bit information of numbers
2022-06-26 06:10:00 【Laughing_ Xie】
The bottom layer of data is composed of binary , How to print int and long Bit information of type number ?
int The type is basic data type , Occupy 4 byte ,32 position .long The type is also the basic data type , Occupy 8 byte ,84 position .
It needs to be used here “&” And operation .
& : And operation , When two numbers perform this operation , Will compare binary information at the bottom , Compare values one by one , Then return the newly generated data after calculation . The rule is : Compare binary data , If both numbers are 1, The location of the generated new data is 1; If the values of the two data in this position Not all for 1 Then assign this position to 0.
example :
@Test
public void test2() {
int a = 123; //00000000000000000000000001111011
int b = 1340; //00000000000000000000010100111100
// Both positions are 1 Will come out 1, Otherwise everything else is 0
printI(a&b); // Print bit information through customized methods
System.out.println(a&b); //56
//00000000000000000000000000111000
}combination "<<" ( Move left ) An operation , Compare them one by one , Get the results , Then print it out .
1 Bit information for : 00000000000000000000000000000001 , Give Way 1 Shift left one by one To compare with the data
private void printI(int num) {
for (int i = 31; i >= 0; i--) {
// & And operation (0 And 0,0 And 1 Meet as 0)
// Because of this characteristic , Left shifted digits and num And operation , If not 1, Then we get 32 position 0, Values for 0
System.out.print((num & (1 << i)) == 0 ? "0" : "1");
}
System.out.println();
}For the first time 1 Move left 31 position , Get to the far left ,10000000000000000000000000000000, Go with this num Operation and operation , If this position is not 1, Will return 00000000000000000000000000000000, The result is 0, Now print 0, If not for 0, Then print 1, You can get the integral number Bit information .( If 1, The new number returned must not be 0).
Print 64 Bit information is also a principle :
// Print 64 Bit information Using and & Operation implementation
private void printL(long num) {
for (int i = 63; i >= 0; i--) {
System.out.print((num & (1L << i)) == 0 ? "0" : "1");
}
System.out.println();
}
@Test
public void testLong() {
long start = System.nanoTime();
long num1 = 1L;
long num2 = 2L;
long num = 123L;
printL(num1);
printL(num2);
printL(num);
long end=System.nanoTime();
System.out.println(end-start);
/*
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000010
0000000000000000000000000000000000000000000000000000000001111011
*/
}& And operation : Two numbers “&” And operation , Compare binary data , If the two numbers are different, it returns 0, Both numbers are 1 Then this position returns a 1.
| Or operations : Two numbers “|” Or operations , Compare binary data , If I have a theta 1, Then this position returns 1.
^ Exclusive or operation : Two numbers “^” Exclusive or operation , Compare binary data , If the two numbers agree , Then this position returns 0, If it's not consistent , This position returns 1
// or | Exclusive or ^
@Test
public void test2() {
int a = 123;
int b = 1340;
printI(a);
printI(b);
System.out.println("===============");
// Two numbers or operations , As long as there is a place 1, Everything else has changed 1
printI(a | b);
// Both positions are 1 Will come out 1, Otherwise everything else is 0
printI(a & b);
// The two positions are the same for 0 Different for 1
printI(a ^ b);
/*00000000000000000000000001111011
00000000000000000000010100111100
===============
00000000000000000000010101111111
00000000000000000000000000111000
00000000000000000000010101000111*/
}
An operation :<< Shift left without sign ( A right shift is a signed right shift )
// An operation Shift left And & And operation , All left shifts are unsigned , Only the right shift is signed
// The number is shifted one bit to the left , amount to num*2
@Test
public void testLeft() {
// Left shift operation 1 Move to the left 31 position , Remaining use 0 Fill up such as 1<<31
//1 Move to the left 1 position , The rest is for 0 completion by 2
System.out.println(1 << 1);
// Move left 2 position position 4
System.out.println(1 << 2);
System.out.println(1 << 31);
// & And operation
System.out.println(2 & (1 << 0));
// The operation is :2 Of 0 Power +2 Of 1 Power +2 Of 2 Power ,1 only one 1,2 Of 0 Power
int num = 1;
int num1 = 2345;
printI(num);
printI(num << 1);
printI(num1);
printI(num1 << 1);
}~ Take the opposite ,0 Turn into 1,1 Turn into 0 , The leftmost position is the sign bit .
~a+1 = -a With this logic , Easy to calculate , All subsequent operations use a set of logic .
// Integer maximum and How do positive and negative numbers represent ?32 Bits can represent at most 40 More than 2 Of 32 Power -1;( because 0 To count , So the maximum value is -1)
//0 Belonging to a nonnegative region , Negative minimum The absolute value can reach 2 Of 31 Power , Reverse the last +1, It takes up the sign bit .
// actually int At most 20 More than 2 Of 31 Power -1 , Because there are positive and negative , If the unsigned number , You can use them all 40 More than , So the unsigned maximum is larger than the signed maximum
@Test
public void test() {
// Positive type The real range of values is 0 A to 30 The range of bits , Keeping the highest bit can mean positive , It can also mean negative
int max = Integer.MAX_VALUE;
System.out.println(max); //2147483647
printI(max); //01111111111111111111111111111111
// The highest position is 0 Then this number must not be negative
int min = Integer.MIN_VALUE;
System.out.println(min); //-2147483648
printI(min); //10000000000000000000000000000000
System.out.println("01111111111111111111111111111111".length());
//int The range is -2 Of 31 Power ~2 Of 31 Power -1
// If it's a negative number , The sign bit must be 1, All numbers other than sign bits are negated , At the end of +1, Then it indicates the value
printI(-1); //11111111111111111111111111111111 Other than sign bit , All reversed 1 Turn into 0...0; At the end of +1 Later value Is the specific value ;
printI(-2); //11111111111111111111111111111110
printI(-3); //11111111111111111111111111111101 Negate all as 10000000000000000000000000000010 +1 ->10000000000000000000000000000011
}
// Take the opposite ; The value of a negative number is Take the opposite +1
@Test
public void test1() {
int a = 1349811;
// Reverse operation
int b = -a;
// Print 32 digit
printI(a); //00000000000101001001100010110011
printI(b); //11111111111010110110011101001101 All the contrary , After reverse +1 Is the value
// Reverse operation +1 , It's negative
int b1 = (~a + 1);
printI(b1); //11111111111010110110011101001101
// Inverse of the integer minimum And 0 Take the opposite
System.out.println("=== Negate the minimum +1 And 0 The opposite is oneself =====");
int min = Integer.MIN_VALUE;
printI(min);
printI(~min); // Take the opposite
// Take the opposite +1
printI(~min + 1);
printI(0);
printI(-0);
/*
=== Negate the minimum +1 And 0 The opposite is oneself =====
10000000000000000000000000000000
01111111111111111111111111111111
10000000000000000000000000000000
00000000000000000000000000000000
00000000000000000000000000000000
*/
}边栏推荐
猜你喜欢
随机推荐
Day2- syntax basis and variables
numpy.tile()
MySQL-08
Logstash——使用throttle过滤器向钉钉发送预警消息
Typora activation method
Five solutions across domains
302. minimum rectangular BFS with all black pixels
Adapter mode
技术能力的思考和总结
消息队列-功能、性能、运维对比
Yamaha robot splits visual strings
The interviewer with ByteDance threw me an interview question and said that if I could answer it, other companies would have an 80% chance of passing the technical level
Logstash——Logstash向Email发送告警邮件
MySQL database-01 database overview
Detailed explanation of serial port communication principle 232, 422, 485
Cython入门
Pytorch (network model)
numpy. frombuffer()
如何设计好的技术方案
Volatile application scenarios









