0

Can you please help me find a way to add zero's to the left of a binary string, here's what I thought would work but it just prints the binary without 0's.

package flipping_bits;
import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    int cases = input.nextInt();        //Número de casos

    int[] dec_nums=new int[cases];      //Arreglo contenedor de casos
    for (int i=0;i<cases;i++){
        dec_nums[i]=input.nextInt();    //Almacenamiento de casos
    }

    int[] bin_nums=new int[cases];  //Arreglo con dec-bin
    for (int i=0;i<cases;i++){              
        bin_nums[i]=Integer.parseInt(String.format("%032d",(Integer.parseInt(Integer.toBinaryString(dec_nums[i]))))); //Convertir cada decimal en el arreglo a binario
    }


    //Imprimir binarios
    for (int i=0; i<cases;i++){
        System.out.println(bin_nums[i]);
    }

}
}
2
  • can you provide the file input you are reading from? Also I'm assuming this is from some algorithm training site like hackerrank.com. If so you can check the discussions on there Commented Nov 28, 2016 at 21:43
  • it's from [hackerrank.com/challenges/flipping-bits] I already checked the discussions section and nobody did it using Java. Well, I didn't find a code that I could understand as I have to then explain to my professor how I solved it. Commented Nov 28, 2016 at 22:59

2 Answers 2

1

If I understand your question correctly, just change bin_nums from an int[] to a String[] and don't parse the integer that you formatted, you will end up with a 32 bit representation of an integer

public static void main(String[] args) {


    Scanner input = new Scanner(System.in);

    int cases = input.nextInt();        //Número de casos

    int[] dec_nums=new int[cases];      //Arreglo contenedor de casos
    for (int i=0;i<cases;i++){
        dec_nums[i]=input.nextInt();    //Almacenamiento de casos
    }

    String[] bin_nums=new String[cases];  //Arreglo con dec-bin
    for (int i=0;i<cases;i++){              
        bin_nums[i]=(String.format("%032d",(Integer.parseInt(Integer.toBinaryString(dec_nums[i]))))); //Convertir cada decimal en el arreglo a binario
    }


    //Imprimir binarios
    for (int i=0; i<cases;i++){
        System.out.println(bin_nums[i]);
    }

}

This is my test case with the result:

3
1
2
3
00000000000000000000000000000001
00000000000000000000000000000010
00000000000000000000000000000011

But assuming you might be trying to solve some algorithm question which usually needs you to solve it in an optimal way, this might not do the trick and may exceed the time limit.

It seems like an exercise because of the class named Solution and the style of the code , eg: test-case, number of test cases ...

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! That's exactly what I wanted. And yes, it's actually from HackerRank's algorithms section, flipping bits specifically. After this, I have to flip all the 32 bits and print the final number in decimal. What would you suggest me?
I suggest you learn bit-wise operations basics and "cheats" . catonmat.net/blog/low-level-bit-hacks-you-absolutely-must-know
0

Add the 0 in front of each binary number in you array with the following example:

bin_num[0] = thisBinarynumber updatedNumber = '0' + thisBinarynumber

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.