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 4117298
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:49:03+00:00 2026-05-20T22:49:03+00:00

I have a counter on a 16 bits field that is incremented/decremented over the

  • 0

I have a counter on a 16 bits field that is incremented/decremented over the time by a hardware peripheral.
I sample its value periodically to sum the difference into a 32bits field.

My problem is to detect the overflow/underflow of the 16 bits field when computing the difference.

Let’s take an example:
At sample n-1, the counter value Vn-1 is 65530.
As sample n, the counter value Vn is 4.
The counter has been incremented by 10. But the difference (Vn – Vn-1), will be something like 65529 (not sure of the exact value).

The only way I found to detect this overflow is to compare the difference value to a fixed value greater than the max increment (I choose 10000).
Do you know a solution to manage this overflow without comparing to this subjective value?

Here is a code example:

static sint32 overallCount = 0;
sint32 diff;
static sint16 previousValue = 0;
sint16 currentValue;

currentValue = sampleValue();

diff = ((sint32) currentValue) - previousValue;
if(diff > 10000) {
    diff -= 65536;
} else if ((-diff) > 10000) {
    diff += 65536;
}

overallCount += diff;
  • 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-20T22:49:03+00:00Added an answer on May 20, 2026 at 10:49 pm

    My previous answer had some mistakes so I’ve rewritten it, but the idea is the same, use unsigned types correctly.

    Make currentValue and previousValue both unsigned integers of the chosen size (e.g. uint16_t). Then merely subtract them. Since the difference will be implicitly promoted to int if int is a larger type than uint16_t, you’ll need to cast or implicitly convert the result back to uint16_t. So:

    static uint16_t previousValue;
    uint16_t currentValue = sampleValue();
    uint16_t diff = currentValue - previousValue;
    

    This makes use of the implicit conversion in the assignment, but you could cast if you like.

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

Sidebar

Related Questions

I have a counter that make counter++ every time one image touches another image.
I have a counter of menu items, that basically add incremental value as a
I have a counter hash that I am trying to sort by count. The
I have a counter that counts up every 1 second and add 1 to
I have an app that collects Perfmon counter values through the API exposed in
I have a simple django page that has a counter on it. I use
Suppose that you have 10 stations that are sending packets at a given time
I want to have a counter that counts down from 60 seconds to 0.
I would like to have a counter, that counts backwards, and is stopped if
I have a counter that's used by multiple threads to write to a specific

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.