// java
int d = 25;
int x = d >> 1 + 1;
System.out.println(x) ; //output: 6
// swift(4)
let d = 25
let x = d >> 1 + 1
print(x) //output: 13
What is a quick code solution?
d >> (1+1)
This is 6.
Swift is calculating
(d >> 1 ) + 1
This is 13.
You can use parentheses to specify the calculation to be performed.
This is because the operator takes precedence:
< p>> in Swift –
https://developer.apple.com/documentation/swift/operator_declarations – <
I tried to convert my code from java to swift, but when using the Int type to execute the shift right operator When, get 2 diffrence results.
// java
int d = 25;
int x = d> > 1 + 1;
System.out.println(x); //output: 6
// swift(4)
let d = 25
let x = d >> 1 + 1
print(x) //output: 13
What is a fast code solution?
Java is calculating
d >> (1+1)
This is 6.
Swift is calculating
(d >> 1) + 1
This is 13.
You can use parentheses to specify the calculation to be performed.
This is because the operator takes precedence:
>In Swift –
https: //developer.apple.com/documentation/swift/operator_declarations – << come before.
> in Java – https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html – before< <