Why convert the right operator and Java in SWIFT to get the difference result

I am trying to convert my code from java to swift, but when using the Int type to execute the shift right operator, I 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 quick 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:

< p>> in Swift –
https://developer.apple.com/documentation/swift/operator_declarations – <> in Java – https://docs.oracle.com/javase/ tutorial/java/nutsandbolts/operators.html – before <<

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< <

Leave a Comment

Your email address will not be published.