0

in java

System.out.println((byte) (31330 >> 4));

output : -90

in javascript

console.log(31330 >> 4);

output : 1958

how to get output -90 in javascript like cast (byte) in java?

asked May 22, 2021 at 18:55
2
  • do you mean like this ``` console.log(~~(31330 >> 4)); ``` it still return 1958 Commented May 22, 2021 at 18:59
  • I don't know how to get the result you're looking for. However, the explanation for this is the fact that JavaScript bit shift operators (<< and >>) work with signed 32-bit integers. There isn't exactly a byte type in JavaScript. Commented May 22, 2021 at 19:00

1 Answer 1

2

In JS there's no byte type, but we've got "array of bytes", so you can create an array from your expression and then pick the first element:

console.log(new Int8Array([31330 >> 4])[0])

answered May 22, 2021 at 19:12
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.