-2

Working on Array Buffer in nodejs, I read about using views over ArrayBuffer. When we use Int8Array view over nodejs buffer and try to assign 128 to it, the result is that -128 gets displayed in output. Here is the code snippet:

let buffer = new ArrayBuffer(16);
let view = new Int8Array(buffer);
view[0] = 128;
console.log(view);

I am confused about how system could interpret 128 as -128. At some places, I found that since leftmost bit in binary representation of 128 is 1, system treats it as negative number. But if we are using leftmost bit as signed bit, I think we should not use that bit in calculation. So I need clarity about how 128 ultimately shown as -128

  • It's not clear what you mean by "*I think we should not use that bit in calculation.*" – Bergi Sep 28 '22 at 00:12
  • 7
    int8 has a range from -128 to 127 so ... 128 is not representable in an int8 ... 128 in binary is 10000000 .... in a signed 8 bit integer int8 that's -128 – Jaromanda X Sep 28 '22 at 00:12
  • Generally there is *no* 128 for 8bit signed integers. Where did you read otherwise? Did you expect 0 or some other value than -128 instead? – Alexei Levenkov Sep 28 '22 at 00:12
  • Given the range of `int8` goes from -128 to +127, it's unclear what else you would expect to happen when you attempt to store +128 in the array. – Bergi Sep 28 '22 at 00:13
  • @Bergi Actually According to me what happens is when we try to put 129 in above code, we will see -127 in output. this is justified because 129 = 10000001. Since leftmost bit is 1, system could treat it as negative number and take 2's complement of rightmost 7 bits (excluding leftmost bit). 2's complement of 0000001 = 1111111 , which is 127. Since leftmost bit is 1. It is -127. But this logic does not fit with 128. Hope it clarifies my question – Satvik Singh Sep 28 '22 at 00:22
  • @JaromandaX yes. therefore I am expecting 0 for 128. – Satvik Singh Sep 28 '22 at 00:28
  • 1
    signed ints usually work like this in 90% of languages ... `n` bits signed integer gives a range of `-2^(n-1)` to `2^(n-1) - 1` – Jaromanda X Sep 28 '22 at 00:29
  • 4
    the fact that you *expect 0 for 128* means you don't understand 2's compliment binary - [here's a simple explanation](https://en.wikipedia.org/wiki/Two%27s_complement) – Jaromanda X Sep 28 '22 at 00:31
  • performing the "2's compliment" operation on an n-bit signed int, you perform the operation on ALL n bits, not n-1 and remember what the msb was before you started ... no ... you do invert, add 1 (treating the value as unsigned when you add the 1 so that zero becomes zero) I think you possibly misunderstand that the operation (invert add 1) is performed without regards considering the value to be signed - the fact that the value represents a signed integer is irrelevant to the operation used to negate it – Jaromanda X Sep 28 '22 at 00:40
  • @JaromandaX Ok Understood. Actually i thought only n - 1 bits are used. Thank you. – Satvik Singh Sep 28 '22 at 00:42
  • Does this answer your question? [Why is the range of bytes -128 to 127 in Java?](https://stackoverflow.com/questions/3621067/why-is-the-range-of-bytes-128-to-127-in-java) – PeterJames Sep 28 '22 at 10:28

1 Answers1

0

Its because Int8Array value range is -128 to 127

Notice how it handles both values.

let buffer = new ArrayBuffer(16);
let view = new Int8Array(buffer);
view[0] = 127;
view[1] = 128;
console.log(view);

enter image description here

Resources:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int8Array

TheoNeUpKID
  • 763
  • 7
  • 11