What's the difference between Go and Javascript about 0xFFFFFFFF ^ 97

1

I want to change Go code to Javascript code, but there is one mistake about result that is different between Go and Javascript. I want to change javascript's result to same as Go's result (0xFFFFFFFF ^ 97)

I try to debug it, and I recognize that 0xFFFFFFFF ^ 97 , in Go is 4294967198 but in javascript it is -98.

In Go:

number1 := 0xFFFFFFFF
number2 := 97
fmt.Print(number1 ^ number2) // 4294967198 

In Javascript:

var number1 = 0xFFFFFFFF
var number2 = 97
console.log(number1 ^ number2) // -98

0xFFFFFFFF ^ 97 is different result in Go and Javascript

javascript
node.js
go
math
asked on Stack Overflow Jun 17, 2019 by DeeLMind • edited Jun 17, 2019 by Jack Bashford

1 Answer

4

In JavaScript, a bitwise operation (^ is bitwise XOR) converts the numbers signed 32-bit integer, then back to a double. So after the XOR operation is performed, the number is converted back to its default type - IEEE-754 double-precision binary number - and then the result is given.

number -> 32-bit signed integer -> bitwise operation -> IEEE-754 double-precision binary number

GoLang uses unsigned integers (GoLang spec), or, as pointed out by icza, int64 numbers.

answered on Stack Overflow Jun 17, 2019 by Jack Bashford • edited Jun 17, 2019 by Jack Bashford

User contributions licensed under CC BY-SA 3.0