Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare an unsigned int variable in javascript?

Tags:

javascript

This is what I want:

>> var uint n = 0;
<- undefined (output by firefox dev console, I don't know what this means)
>> n;
<- 0;
>> n - 1;
<- 4294967295 (2^32-1)

So how to declare a variable, in javascript, so that it is unsigned? Thanks in advance.

like image 795
Lukas Kitsche Avatar asked Nov 30 '16 09:11

Lukas Kitsche


People also ask

How do you declare an unsigned int?

The data type to declare an unsigned integer is: unsigned int and the format specifier that is used with scanf() and print() for unsigned int type of variable is "%u".

What is an unsigned int in Javascript?

Unsigned integers have no sign bits and are always non-negative. For example, for BigInt.asUintN(4, 25n) , the value 25n is clamped to 9n : 25n = 00011001 (base 2) ^==== Clamp to four remaining bits ===> 1001 (base 2) = 9n. Note: BigInt values are always encoded as two's complement in binary.

What is unsigned int example?

The simplest numbers that we want to represent in the machine are the unsigned integers. These are whole numbers without a sign, for example, 0, 1, 2, 3, …

Can int be unsigned?

In C, unsigned is also one data type in which is a variable type of int this data type can hold zero and positive numbers. There is also a signed int data type in which it is a variable type of int data type that can hold negative, zero, and positive numbers.


2 Answers

If you really need them unsigned then check Uint32Array on MDN.

However, since you don't really gain anything from your values being unsigned, perhaps a simple modulus operation would be better?

//Create var as array of length 1
var arr = new Uint32Array(1);
//set first value to 1
arr[0] = 1;
//output contents
console.log(arr);
//substract to "negative"
arr[0] -= 2;
//output contents
console.log(arr);

Modulus

//Object with setter, to make it simpler
var my_obj = Object.create({
  //This is our value
  value: 0
}, {
  //This is our setter
  set: {
    value: function(a) {
      //Make incoming value a modulus of some integer
      //I just use 1000 for the example
      var max = 1000;
      a = a % max;
      //Force a positive
      while (a < 0) {
        a += max;
      }
      //Set value
      this.value = a;
      //Return object regerence for chaining
      return this;
    }
  }
});
console.log("0:", my_obj.value);
my_obj.set(500);
console.log("500:", my_obj.value);
my_obj.set(-0);
console.log("-0:", my_obj.value);
my_obj.set(-1);
console.log("-1:", my_obj.value);
my_obj.set(-100);
console.log("-100:", my_obj.value);
my_obj.set(-0);
console.log("-0:", my_obj.value);

Or with a function:

function modulusMax(value, a) {
  //Make incoming value a modulus of some integer
  //I just use 1000 for the example
  var max = 1000;
  a = a % max;
  //Force a positive
  while (a < 0) {
    a += max;
  }
  //Set value
  value = a;
  //Return object regerence for chaining
  return value;
}

var v = 0;

console.log("0:", modulusMax(v, 0));
console.log("500:", modulusMax(v, 500));
console.log("-0:", modulusMax(v, -0));
console.log("-1:", modulusMax(v, -1));
console.log("-100:", modulusMax(v, -100));
console.log("1100:", modulusMax(v, 1100));
console.log("-0:", modulusMax(v, -0));
like image 97
Emil S. Jørgensen Avatar answered Oct 10 '22 14:10

Emil S. Jørgensen


LK"I

Is this what you mean?

var unsigned = someVar >>>0

JavaScript C Style Type Cast From Signed To Unsigned

like image 43
Harel Ashwal Avatar answered Oct 10 '22 14:10

Harel Ashwal