Implement the flipBit method, which changes the value of one bit of a given integer to the opposite. We agree that the bits are numbered from the youngest (index 1) to the oldest (index 32).
public static int flipBit(int value, int bitIndex) { char[] mass = Integer.toBinaryString(value).toCharArray(); int index = bitIndex%32; if(mass[index - 1] == '0') mass[index - 1] = '1'; else mass[index - 1] = '0'; return Integer.parseInt(new String(mass), 2); } Gives an error message:
Failed test # 3. Run time error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 30
at Main.flipBit (Main.java:27)
at Main.main (Main.java:14)
at Main.main (Main.java:7)
value^(1<<(bitIndex-1))? - PetSerAlString.format("%32s", Integer.toBinaryString( value ) ).replace(' ', '0'), cho there :) - zRrr