B
B
Biaci_Anj2021-12-09 22:34:05
Java
Biaci_Anj, 2021-12-09 22:34:05

Please explain this example in bitwise operations, why is Math.pow here?

The meaning of the method is simple: you need to show 64 bits and only 1 where necessary, depending on the parameters that were passed to the method. You can think of these 64 bits as an 8*8 map.

/*
    ABCDEFGH
   ┌────────
  1│10000000
  2│00000000
  3│00000000
  4│00000000
  5│00000000
  6│00000000
  7│00000000
  8│00000000
 */
В метод залетает буква и цифра - А1 например ( как в примере выше ), там и должна быть единица.

    public static void shoot(String shot) {
       int columns = (int) shot.charAt(0) - 64; 
       int rows = Character.getNumericValue(shot.charAt(1));

       int sdvig = 8 * ( 8 - rows);
       long firstValue  = (long) Math.pow(2, 8 - columns );
        System.out.println(Long.toBinaryString(firstValue << sdvig));

}
shoot("A1");

I don't really understand the solution above, but it works.
I would be extremely grateful if you could explain why Math.pow is here

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rPman, 2021-12-10
@Biaci_Anj

exponentiation 2^x is a bitwise shift of one to the left by x bits, counting starts from 0 (2 to the power of 0 = 1, 2 to the power of 1 - 010, 2 to the power of 3 - 100,..)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question