简体   繁体   中英

Can someone explain this type of syntax, and what it is doing?

I am not really sure what is going on in the print statement. More specifically, what does the "&", "?" and ":" do to the formatting in this print statement?

 for (int j = 0; j < x; j++) {
            System.out.print((maze[j][i] & 8) == 0 ? "|   " : "    ");
        }

The '&' here is the bitwise operator. And variable == something ? x : y this is the ternary operator which in the example you can read like this 'If bitwise AND of maze[i][j] with 8 is equal to 0, then print "| " else print " " '

(maze[j][i] & 8) == 0 ? "|   " : "    ")

evaluates to "| " if the 8-bit of maze[j][i] is 0, and " " if it's 1.

maze[j][i] & 8 does a bitwise AND of the value of maze[j][i] and the number 8. The result will be either 8 (if the value at maze[j][i] has a 1 in the bit position corresponding to 8's) or 0 (if it has a 0 there).

If it is zero ( == 0 ), then ( ? ) the expression resolves to the string with the pipe( "| " ); otherwise ( : ) it resolves to the string without the pipe ( " " ).

The & is a bitwise and (it will be 1 when the eighth bit is 1 in both numbers), the == is a boolean conditional and the ?: is the ternary conditional expression (jls-15.25) .

System.out.print((maze[j][i] & 8) == 0 ? "|   " : "    ");

is equivalent to

if ((maze[j][i] & 8) == 0) {
  System.out.print("|   ");
} else {
  System.out.print("    ");
}

Let's say it is a shortcut if/else condition or they call it ternary operator. It's actually a good way when you want to test some value before printing out something or storing something into a variable. & is a bitwise AND operator, it compares the bits of each side. ? is something like THEN, when the condition is true it will do the task inside it, : is else it will do the task inside it if the condition is not true

here's a more readable snippet so you can understand what I'm talking about

for (int j = 0; j < x; j++) {
    if(maze[j][i] & 8) {
        System.out.print("|   ");
    } else {
        System.out.print("    ");
    }
}
System.out.print((maze[j][i] & 8) == 0 ? "|   " : "    ");

translates as:

If the value at maze[j][i] has a 0 in the 4th-least-significant bit, then print "| "; otherwise, print " ".

& is a bitwise and; it returns 0 if there are no 1 bits in common between the operands.

? : is a ternary operator. It evaluates the truth of the first argument. If true, it executes the second argument; otherwise, it executes the third.

& is bitwise and .it returns 1 only when a and b both are 1

a and b are individual bits in which a number can be represented and can have a truth value of 0 or 1

eg for 12 & 13

12 = 1100(in binary)

13 = 1101(in binary)

12&13 = 1100

and the statement x == 0 ? 1 : 2 x == 0 ? 1 : 2 is called ternary operators ..if the statement before ? is true then the first statement (ie) 1 is printed otherwise 2 ..it is equivalent to if else ..it can be written like

if (x == 0) {
   System.out.println(1);
} else {
   System.out.println(2);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM