-3

How can I convert int=43707 to two other numbers?

The first number is made by value of odd bits. Second number is made by value of even bits.

int x = 43707; // 1010101010111011
var even = 0;
var odd = 0;
    
for (int i = 0; i<=31; i++) {
    if(i%2 == 0) {
        ?
    } else {
        ?
    }
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Daniel
  • 1
  • 1
  • is it homework or for what do you need it? I'm just interested in what's the use-case? do you know how to convert an integer to binary representation in Java with `Integer.toBinaryString(int i)`? – UninformedUser Oct 23 '18 at 11:20
  • Can you give the expected result for the ``even`` and ``odd`` numbers ? in binary – Schidu Luca Oct 23 '18 at 11:23
  • 1
    You need to break-down the problem into smaller problems, then tackle each at a time. -How do you know a bitwise number is odd/even? -How do you get a bitwise number from an integer? -How do you iterate over all digits in a bitwise number? etc. Each of these problems is easily found via Google + Stackoverflow. Hint: Java's bitshift and bitmasking operations will be useful. – SME_Dev Oct 23 '18 at 11:24
  • As Schidu Luca said, its not quite clear what you mean with the two numbers. If you just want to filter out even and odd bits and don't want to change their positions you can use a bitmask like `var even = x & 0xaaaa; var odd = x & 0x5555`. – RobinW Oct 23 '18 at 11:28
  • Every odd int has odd value in binary system and every even int has even value in binary value........i.e 1=1,2=10,3=11,4=100,5=101,6=110...................43706=1010101010111010,43707=1010101010111011.....i think you can break it in one number is 43706 and other number is 1. You need to specify it that you required ? – Syed Hamza Hassan Oct 23 '18 at 11:30
  • For example I need to add 1st 3rd 5th 7th 9th.. bits values. And save it as a veriable. Its a task from study lecture. – Daniel Oct 23 '18 at 11:41

3 Answers3

0

Just convert int into digits as shown below:

List<Integer> digits = new ArrayList<Integer>();
while(x > 0) {
    digits.add(x % 10);
    x /= 10;
}
System.out.println(digits);

Once you have the separated the digits then apply the even odd logic. Here is complete code:

int x = 43707;          // 1010101010111011
List<Integer> digits = new ArrayList<>();
while(x > 0) {
    digits.add(x % 10);
    x /= 10;
}
int i = 0;
int length = digits.size();
while (i < length) {
   if(digits.get(i)%2 == 0){
       System.out.println("Even Number" + digits.get(i));
   } else {
        System.out.println("Odd Number" + digits.get(i));
   }
   i++;
}

If you are looking for the Binary conversion then you can use the below code.

int x = 43707;          // 1010101010111011
int testNumber;
String binaryNumber = Integer.toBinaryString(x);
for (int i = 0 ; i != binaryNumber.length() ; i++) {
    char c = binaryNumber.charAt(i);
    testNumber = Character.getNumericValue(binaryNumber.charAt(i));
    if(testNumber == 0){
        System.out.println("Even Number");
    } else {
        System.out.println("Odd Number");
    }
    System.out.println(c);
}
System.out.println(binaryNumber);

It converts the Int to Binary and then check even and odd numbers.

Hope, it works for you as per your desired output.

Sami Ahmed Siddiqui
  • 2,328
  • 1
  • 16
  • 29
0

I came up to this:

int x = 43707;
String binary = Integer.toBinaryString(x);
System.out.println("binary=" + binary);

String odds = "";
String evens = "";

for (int i = binary.length() - 1; i >= 0; i--) {
    if ((i + 1) % 2 == 0) {
            odds += binary.charAt(i);
        } else {
            evens += binary.charAt(i);
    }
}

System.out.println("odds=" + odds);
System.out.println("evens=" + evens);

int odd = Integer.parseInt(odds, 2);
int even = Integer.parseInt(evens, 2);

System.out.println("number from odd bits=" + odd);
System.out.println("number from even bits=" + even);

prints

binary=1010101010111011
odds=10100000
evens=11111111
number from odd bits=160
number from even bits=255

I'm counting right to left the bits.

forpas
  • 160,666
  • 10
  • 38
  • 76
0

Your looking for the bitwise AND operation: &. you can use it together with a binary mask (normally specified in hex notation 0x00FF). so you need to do something like:

int x= 707; //10110011
int oddBits = 0x5555; //01010101
int evenBits = 0xAAAA; //10101010

int oddResult = x & oddBits;
System.out.println(oddResult);

int evenResult = x & evenBits;
System.out.println(evenResult);

which returns: 65 //00010001 and 642 // 10100010

Dinomaster
  • 256
  • 1
  • 6