Dave's Brain

Browse - Programming Tips - How can I force a number to be unsigned?

Date: 2014oct15
Language: javaScript

Q.  How can I force a number to be unsigned?

A.  Use >>>0 like this:

var a = num >>> 0;
>>> is an unsigned right shift and 0 is the amount to shift. Cute. Normally numbers in javaScript are 64-bit floating point. So you don't overcute future programmers you can make a function:
function makeUnsigned(n) { return n >>> 0; }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.