I am trying to solve a problem that comes down to finding the hamming code without using binary string of the long number in java.
I don't understand how we can do that, I have searched at many places like the wikipedia explanation and this one I found online but all of them need binary string to work with, but here in this case I can't use them.
I have a long number that represents the input binary, now I need to find the hamming code.
As I understand it, I can have the empty places at positions 1, 2, 8 ... in that binary. Now I can put 0 or 1 in that place using the boolean that tells me if this is an even parity or odd.
I wrote a code using string and lists, but I need to this without using strings or lists.
String text = new Scanner(System.in).next() ;
int len = text.length() ;
ArrayList codearray = new ArrayList() ;
ArrayList pos_arr = new ArrayList() ;
while (!(len + r + 1 <= Math.pow(2, r))) {
r++;
}
System.out.println("Number of parity bits : "+r);
for (int i=0 ; i<len + r ; i++) {
if (IsPowerTwo(i+1) ){
codearray.add("?") ;
}else {
codearray.add(text.charAt(j));
j++ ;
}
}
System.out.println(codearray.toString());
//to determine the position of parity bits
for (int i=0 ; i< codearray.size() ; i++) {
if (codearray.get(i) == "?") {
int k ,s=1;
// For Debugging.
//System.out.println("Calculate at position " + (i+1) ) ;
for (k = i ; k < codearray.size() ; k++){
if (i==0) {
k++ ;
pos_arr.add(k);
}else {
pos_arr.add(k+1);
if(s % (i+1) == 0) {
k += i+1 ;
}
}
s++ ;
}
checkOnes(pos_arr,codearray,i) ;
pos_arr.clear();
}
}
System.out.println("Code word: ");
Arr_to_Str(codearray) ;
}
public static boolean IsPowerTwo(int num){
int checked = num & num -1 ;
if (checked == 0 ){
return true ;
}else {
return false ;
}
}
public static void checkOnes(ArrayList array, ArrayList codearray, int position ){
int count =0;
for (int i=0 ; i < array.size(); i++) {
int index = (int) array.get(i) -1 ;
if (codearray.get(index) == "?" ) {
codearray.set(index,0) ;
}
int num = Integer.parseInt(codearray.get(index).toString()) ;
if (num == 1 ) {
count++ ;
}
if(count % 2 ==0 ){
codearray.set(position, 0) ;
}else {
codearray.set(position, 1) ;
}
}
}
public static void Arr_to_Str(ArrayList array){
for (int i=0;i<array.size();i++){
System.out.print(array.get(i));
}
}
But this requires me to work with binary string or list, how can I do the same with long numbers like, say 210.
I am really new to bit manipulation and hence I can't wrap my head around this. I would really appreciate any help.