Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
If I have an integer that will have always a positive number less than 16 , can I just cast it to byte
int i = 5;
byte b = (byte) i;
or should I have unexpected behaviour when converting it back to integer on different devices?
Thanks
No, you won't get unexpected behaviour converting a byte between 0 and 15 into an int on different platforms. One of the strengths of Java is that it precisely defines what happens with such conversions, so that they are always platform independent.
You could always use the Integer class and use byteValue.
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#byteValue()
which simply does
return (byte)value;
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm new learner in java
This is just a part of the code just i need to know why he is using typecasting here and why int specially?
double anynumber =(int) 32/8.5;
This:.
(int) 32/8.5;
casts 32 to an integer and not the result of the division to integer,
because casting has higher precedence than division.
But it is totally unnecessary since 32 is already an integer.
So this:
double anynumber =(int) 32/8.5;
System.out.println(anynumber);
will print:
3.764705882352941
just like if the casting was not there.
But this:
double anynumber =(int) (32/8.5);
System.out.println(anynumber);
will print:
3.0
because it applies the casting to the result of the division by truncating the decimal digits to create an integer.
So to answer your question, if you found this line of code in a book or online,
it does not do anything different than a simple:
double anynumber = 32/8.5;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How can I convert a large binary (10k bits) to a base 3 or a base 31 number?
I know that for base 16 I can convert the number by taking 4 bytes chunks from my original number and this works well. But for base 3 or 31 is this even possible, since they are not a factor of 2?
Edit
I tried to simplify the problem above, but for clarity think that there is a binary stream of 100MB, and you need to convert it in a stream that would be interpreted base 31.
Thanks for the answers so far.
You can't do this in a streaming fashion. For example, if the streams are big-endian, then you'll need to wait until the last bit in the input to distinguish between 31^100001 and (31^100001 - 1), and this difference affects the first bit of the output.
It might not be the fastest, most efficient way to get the job done, but a very easy approach would be to use Java's BigInteger class.
(new BigInteger(hugeBinaryNumberAsString, 2)).toString(3)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am solving a problem for which i require to know the number of 1 bit's in a BigInteger.
Thanks in advance.
You can use .bitCount() on the BigInteger. Unless you need to solve it manually, in which case you can use normal Java bitwise operations.
Do x-or with 0. By this you will get the bits set as 1 wherever there was 1 in your original input. Then you can count the number of bits set in the output.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
it is possible to assign that big number in java? i need to make a calculation of 39 digits value. could any help? Thanks
Problem:
Consider the following composite number:
340282367237851113557325445936183246849
Write a Java method to find two numbers whose product is the above number.
I guess you need to check out the BigInteger of the java API. That might be able to store your results of those much big numbers. Read the documentation,
http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I read some open source codes and often come across variable declaration like this:
private static final int MAX_LITERAL = 1 << 5;
This is 2^5 (32) and is much more readable for some people to declare as "32" than "1 << 5". Any good explanation to why it is done using bit shifting operator?
It's for readability purposes, most likely that number is used in bitwise operations.
Although it is indeed 32, maybe you'll see the reasoning if you don't think of it as 32, but as a number with the fifth bit set, and the others unset.