Given an integer in Java, is there an easy way you could represent this in binary as an array of boolean
?
int input = 12;
return {True, True, False, False};
And what about, for example, if you already had a set list length? How would you make it return a list with the set length?
int input = 15;
return {False, False, True, True, True, True};
I tried to make a pointer that would reset every two times, but I know that there has to be an easier solution involving just using the %
operator.
This resulted in a very long time delay, ending up in a timing out.
CodePudding user response:
You can try using BitSet:
BitSet bitSet = BitSet.valueOf(new long[] { 12});
The order is the opposite of what your example provided though - index 0 is the smallest value. For instance, using 12, the BitSet
has a string representation of {2, 3}
, which means bits 2 and 3 are set, and when using 15, it's {0, 1, 2, 3}
.
CodePudding user response:
What is "some sort of byte"? If you want binary, I got this off of the web:
import java.util.*;
import java.math.*;
class GFG {
public static BigInteger binaryConv(BigInteger n)
{
if (n.compareTo(BigInteger.valueOf(1)) == 0) {
return BigInteger.valueOf(1);
}
return ((binaryConv(n.divide(BigInteger.valueOf(2))).multiply(BigInteger.valueOf(10))).add(n.mod(BigInteger.valueOf(2))));
}
public static void main(String[] args)
{
BigInteger N = new BigInteger("9876543210987543210");
System.out.println(binaryConv(N));
}
}
Output:
1000100100010000100001111011100011100011101101010101101010101010
If this doesn't help, I feel you need to restate your question.
P.S. Java doesn't have pointers. Theoretically, you could make one, but don't; they're not there for a reason.
CodePudding user response:
Given an integer in Java, is there an easy way you could represent this in binary as an array of booleans?
Interger
class provides a static method Integer.asBinaryString(i)
, which returns "string representation of the integer argument as an unsigned integer in base 2".
Note that the returned string would not contain leading zeros (judging by the first example, that matches the behavior you need).
Alternatively, the same string can be obtained by using Integer.toString(i,2)
which expects an integer number and a radix.
That's how such string can be converted to a boolean
array:
public static boolean[] toBooleanArray(int num) {
String binaryStr = Integer.toBinaryString(num);
boolean[] res = new boolean[binaryStr.length()];
for (int i = 0; i < res.length; i )
res[i] = binaryStr.charAt(i) == '1';
return res;
}
Or if you're OK with an array of wrapper type Boolean[]
, it can be generated in a single statement using Stream API (since input is an integer array would contain at most 31 element, so it would not a lot of difference this array is needed only for one use and method is not intended to be used very frequently):
public static Boolean[] toBooleanArray(int num) {
return Integer.toBinaryString(num).chars()
.map(Character::getNumericValue)
.mapToObj(i -> i == 1)
.toArray(Boolean[]::new); // or toList() to obtain the result as list
}
main()
public static void main(String[] args) {
System.out.println(Arrays.toString(toBooleanArray(9)));
System.out.println(Arrays.toString(toBooleanArray(12)));
System.out.println(Arrays.toString(toBooleanArray(15)));
}
Output:
[true, false, false, true] // 9 - 1001
[true, true, false, false] // 12 - 1100
[true, true, true, true] // 15 - 1111