I have a tree in the client, in javascript:
function Node(uuid, someData, versionNum) {
this.uuid = uuid;
this.someData = someData;
this.versionNum = versionNum;
this.childNodes = [];
}
and the same tree in the server, in java:
public class Node {
UUID uuid;
String someData;
int versionNum;
List<Node> childNodes;
}
The client will send a request to the server every five seconds, asking for a hash of the tree. The idea is that the tree's hash will be recursively calculated like this:
public static long hashSubtree(Node node) {
long hash = node.uuid.getMostSignificantBits() ^ node.uuid.getLeastSignificantBits() ^ node.versionNum;
for (Node childNode : node.childNodes)
hash ^= hashSubtree(childNode);
return hash;
}
On the client, once it receives the response from the server, with the server's calculated hash, the client will then calculate its own hash of its local tree:
function hashSubtree(node) {
var hash = getMostSignificantBitsAsInt(node.uuid) ^ getLeastSignificantBitsAsInt(node.uuid) ^ node.versionNum;
for (var i = 0; i < node.childNodes.length; i++)
hash ^= hashSubtree(node.childNodes[i]);
return hash;
}
and then the client will compare the two hash codes. If the two hash codes are different, then the client is out of sync with the server, and will request the entire tree.
The question:
Since precision is of absolute importance, I need to make sure that the javascript is always dealing in integers, and never converts anything to floats. Is it save to assume that if I just keep on using xor like this, then it will never become a float?
Or perhaps there's just a better way of doing this than hashing with xor to compare the trees?