Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7591333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:37:36+00:00 2026-05-30T20:37:36+00:00

I want to subtract two signed 8bit integers in JavaScript. As JavaScript doesn’t have

  • 0

I want to subtract two signed 8bit integers in JavaScript. As JavaScript doesn’t have a type for ‘signed’ or ‘unsigned’ integers I need to stick to the ‘number’ type – and cannot do some ‘cast-magic’ just like it would be possible using different languages.

I want to simulate a kind of CPU register and therefore need to work on as well the signed as the unsigned values. Additionally I need to calculate the values of the Carry and the Overflow flag. The Carry Flag is set when an unsigned overflow happens (the unsigned value has left the range from 0 to 255) while the Overflow Flag is set on a signed overflow (value is not is not in range -128 – 127).

This code seems to work but is quite cumbersome and ugly. Is there any “easy” way of doing this calculation?

Thanks in advance!

//Convert a signed 8bit number to a "real" number (254 => -2)
function toSigned(number)
{
    if (number & 0x80) //sign bit is set
        number = -(0x100 - number);
    return number
}
//Inverts the sign of a signed 8bit number
function invertSign(number)
{
    return 0x100 - number;
}
//subtracts source from target
function sub(target,source)
{
    //calculate carry
    var carry = (target - source < 0);
    console.log("Carry: %b", carry);

    //Calculate result & overflow
    var result = (target + invertSign(source)) & 0xFF;
    var overflow = !(result >= -128 && result <= 127);
    console.log("Overflow: %b", overflow);
    console.log("Result: %d - %d = %d",toSigned(target),toSigned(source),result);
}
//Examples
sub(8,254); //8 - (-2), no overflow, carry set
sub(8,2); //8 - 2, no overflow, no carry
sub(0x50,0xB0); //80 - (-80), overflow, carry
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:37:37+00:00Added an answer on May 30, 2026 at 8:37 pm

    It seems that one of best options for you is Typed Arrays (it’s not completely supported by all browsers, see Can I Use: typedarrays)

    var signed8bitArray = new Int8Array(1);
    var unsigned8bitArray = new Uint8Array(1);
    
    unsigned8bitArray[0] = 254;
    signed8bitArray[0] = unsigned8bitArray[0];
    signed8bitArray[0] == -2 // This is true
    

    You may want to read Int8Array, Uint8Array and general documentation on JavaScript typed arrays from MDN.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays which contain objects of assets, now I want to subtract
I have two unix timestamps as LONG INT. I want to subtract start from
Hi I have two arrays containing 4 columns and I want to subtract the
I have prices in two different tables and want to subtract them (current price-last
I have two floats in Python that I'd like to subtract, i.e. v1 =
I have a database for a Train company. I want to combine two queries
I have two nsdatecomponent object, and I want a substract the time of my
I want a function, subtract, that will take two arrays, and return all of
I want to subtract two time periods say 16:00:00 from 19:00:00. Is there any
I have two dates as duedate as 03/03/2011 and returndate as 03/09/2011. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.