Java Big Integer Value

조회 수: 10 (최근 30일)
Dyuman Joshi
Dyuman Joshi 2022년 2월 1일
댓글: Dyuman Joshi 2022년 2월 15일
I was working with BigIntegers to solve a problem on Cody, where I got stuck upon something.
import java.math.*
BigInteger(10)
ans = 10
BigInteger(1234)
ans = -46
BigInteger(num2str(1234))
ans = 1234
I tried to find why this happens but I couldn't find any useful resources. I have no knowledge of Java if anyone needs to know.
Is the default input to BigInteger a string/character array? (Which I suspect so)
Also an odd behaviour (which I don't understand)? Can someone explan why this happens?
for i=1:11
BigInteger(2^i+1)
end
ans = 3 ans = 5 ans = 9 ans = 17 ans = 33 ans = 65 ans = -127 ans = 1 ans = 1 ans = 1 ans = 1

채택된 답변

Walter Roberson
Walter Roberson 2022년 2월 1일
The single-parameter form of BigInteger with a numeric parameter is
"BigInteger(byte[] val)
Translates a byte array containing the two's-complement binary representation of a BigInteger into a BigInteger."
So when you pass in a numeric value, it converts the numeric value to a signed byte.
When you pass in the result of num2str() you are using a different constructor,
Translates the decimal String representation of a BigInteger into a BigInteger."
  댓글 수: 5
Walter Roberson
Walter Roberson 2022년 2월 15일
One byte holds 8 bits, and 2^8 = 256 .
The BigInteger class always uses full bytes -- you cannot define a BigInteger that is (for example) 11 bits long
Dyuman Joshi
Dyuman Joshi 2022년 2월 15일
That makes it clear, thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by