Reputation: 716
I have a function for the value of a number:
val = sum{(A[i])*(-2)^i}
So representing values in an array of bits can be done s.t. -8
is [0,0,0,1]
and 8
is [0,0,0,1,1]
.
Please help me to write a java method for, when I pass a decimal value to a method it will return a int array with 1 and 0 with represent the given decimal value.
Ex: when pass -8 , method returns [0,0,0,1].
when pass 8 , method returns [0,0,0,1,1].
Upvotes: 0
Views: 2106
Reputation: 10633
-8 in base -2 should be [0,0,0,1]
not [0,1,0,1]
according to definition val = sum{(A[i])*(-2)^i}
Following is the code to deal with the negative bases
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Binary {
public static void main(String[] args) {
System.out.println("Started");
Scanner scan = new Scanner(System.in);
System.out.println("Enter an integer : ");
int num = Integer.parseInt(scan.nextLine());
System.out.println("Enter base to convert to: ");
int base = Integer.parseInt(scan.nextLine());
scan.close();
Integer[] bitArray = convertToBaseArray(num, base);
System.out.println(Arrays.toString(bitArray));
}
private static Integer[] convertToBaseArray(int num, int base) {
if(base == 0) {
throw new RuntimeException("base can not be 0");
} else if(num < 0 && base > 0) {
throw new RuntimeException("positive base can not produce negative number");
}
List<Integer> bitList = new ArrayList<Integer>();
int absoluteBase = Math.abs(base);
if(base == 1) {
//this is not unique, creating shortest bit array
for(int i = 0; i < Math.abs(num); i++) {
bitList.add(1);
}
} else if(base == -1) {
//this is not unique, creating shortest bit array
int evenBit = num > 0 ? 0 : 1;
int oddBit = 1-evenBit;
for(int i = 0; i < 2*Math.abs(num); i+= 2) {
bitList.add(oddBit);
bitList.add(evenBit);
}
if(oddBit == 1) {
bitList.remove(bitList.size() -1);
}
} else {
while(num != 0) {
int remainder = num % base;
if(remainder < 0) {
remainder += absoluteBase;
num -= absoluteBase;
}
num = num / base;
bitList.add(remainder);
}
}
return bitList.toArray(new Integer[]{});
}
}
Here are some sample runs :
Started
Enter an integer :
-8
Enter base to convert to:
-2
[0, 0, 0, 1]
Started
Enter an integer :
8
Enter base to convert to:
-2
[0, 0, 0, 1, 1]
Started
Enter an integer :
8
Enter base to convert to:
-5
[3, 4, 1]
Started
Enter an integer :
4
Enter base to convert to:
1
[1, 1, 1, 1]
Started
Enter an integer :
4
Enter base to convert to:
-1
[1, 0, 1, 0, 1, 0, 1]
Started
Enter an integer :
-4
Enter base to convert to:
-1
[0, 1, 0, 1, 0, 1, 0, 1]
Upvotes: 5