简体   繁体   English

Java.math.BigInteger.and()如何工作?

[英]How does Java.math.BigInteger.and() work?

According to this tutorial the result from Java.math.BigInteger.and() when calling .and() with values 6 and 3 is 2. How does this work? 根据本教程 ,使用值6和3调用.and()时,Java.math.BigInteger.and()的结果为2。这是如何工作的? It's neither concatenating nor adding. 它既不连接也不添加。

Logical bitwise And operation 逻辑按位与运算

6 = 110b (b means binary) 6 = 110b (b表示二进制)
3 = 011b

110b & 011b = 010b = 2

AND Truth table is following for 2 inputs AND真值表为2输入

在此处输入图片说明

so for two integers 3 and 4 所以对于两个整数3和4

3 = 011b
4 = 100b

b means in base 2. b表示以2为底。

The and operation of 3&4 = 011&100. 3&4 = 011&100.的和运算3&4 = 011&100.

so applying AND truth table for each bit position we get 所以对每个位位置应用AND真值表

1&0 = 0

1&0 = 0

0&1 = 1

Actually, I am applying and operation LSB to MSB for both numbers. 实际上,我正在为这两个号码向MSB应用并操作LSB。

Now Biginteger class is introduced for calculating the large number which does not fit primitive data types long int . 现在,引入了Biginteger class ,用于计算不适合long int原始数据类型的大数。

BigInteger will do the same operation but for too big numbers. BigInteger将执行相同的操作,但是数量太大。

Its bitwise And operation.In your question given link shows, 它的按位与运算。在您的问题中,给定的链接显示为:

110 & 011 = 010
110 is the binary value of 6.
011 is the binary value of 3.

The result 010 is the binary of 2(decimal) . 结果0102(decimal)的二进制。

It does the & (Bitwise And). 它执行&(按位与)。 Which means it returns the matching 1's in its binary representation. 这意味着它将以二进制表示形式返回匹配的1。 For example, 6 is 0110, 3 is 0011 so: 6 & 3 is 0110 & 0011 = 0010 = 2 例如,6是0110,3是0011,所以:6&3是0110&0011 = 0010 = 2

BigInteger in java is same like primitive types int and long.All Bitwise operations that you are doing on numeric primitive types is same as and(),or(),xor() methods of BigInteger.BigInteger is like unlimited integer there is no fixed memory for it (for example int is 4 bytes Minimum value is - 2,147,483,648.(-2^31) Maximum value is 2,147,483,647(inclusive).(2^31 -1)).Your BigInteger is internally stored as Java中的BigInteger与原始类型int和long相同,您在数值原始类型上执行的所有按位运算与BigInteger的and(),or(),xor()方法相同.BigInteger就像无限整数一样,没有固定值它的内存(例如int是4个字节,最小值是-2,147,483,648。(-2 ^ 31)最大值是2,147,483,647(含)。(2 ^ 31 -1))。BigInteger在内部存储为

final int[] mag; final int [] mag;

For and() method If your BigInteger value is more than int type it will add to array.Bitwise operations are done between array of interger on both inputs for example you have two bigintegers x and y 对于and()方法,如果您的BigInteger值大于int类型,它将添加到数组中。按位运算是在两个输入的整数数组之间进行的,例如您有两个bigintegers x和y

It will calculate like x.mag[0] & y.mag[0] x.mag[1] & y.mag[1] . 它将像x.mag [0]和y.mag [0] x.mag [1]&y.mag [1]一样进行计算。 . Add those all values returned by each and sum it up and give you final value.Finally its same like Bitwise operations &,|; 最后,将它们各自返回的所有值相加并求和,得出最终值。最后,与按位运算符&,|;相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM