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.
Related
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 months ago.
This post was edited and submitted for review 9 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have integer status response from a REST API. I need to return string, so I use it like
return "" + statusCode
Is it fine way to do that or is it costly? Or should I use String.valueOf() or is there another alternative?
No need - if you decompile the class file, you'll see that the compiler does this for you anyway. I think this makes the code very readable, however you might want to ask yourself why are you doing this anyway... if you are confident that returning a number as a string from a method is correct, this is a good way to do it, IMO.
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 7 years ago.
Improve this question
Why do we have int type in Java? Couldn't the only numeric type be double (and maybe float)? You can keep any integer number in a variable of type double.
For performance reasons and memory saving. Plus , the fact that the numbers are precise with no decimal values is very handy for a lot of cases.
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 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;