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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:49:40+00:00 2026-05-26T01:49:40+00:00

For unsigned int x, is it possible to calculate x % 255 (or 2^n

  • 0

For unsigned int x, is it possible to calculate x % 255 (or 2^n – 1 in general) using only the following operators (plus no loop, branch or function call)?

!, ~, &, ^, |, +, <<, >>.

  • 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-26T01:49:41+00:00Added an answer on May 26, 2026 at 1:49 am

    Yes, it’s possible. For 255, it can be done as follows:

    unsigned int x = 4023156861;
    
    x = (x & 255) + (x >> 8);
    x = (x & 255) + (x >> 8);
    x = (x & 255) + (x >> 8);
    x = (x & 255) + (x >> 8);
    
    //  At this point, x will be in the range: 0 <= x < 256.
    //  If the answer 0, x could potentially be 255 which is not fully reduced.
    
    //  Here's an ugly way of implementing: if (x == 255) x -= 255;
    //  (See comments for a simpler version by Paul R.)
    unsigned int t = (x + 1) >> 8;
    t = !t + 0xffffffff;
    t &= 255;
    x += ~t + 1;
    
    // x = 186
    

    This will work if unsigned int is a 32-bit integer.

    EDIT: The pattern should be obvious enough to see how this can be generalized to 2^n - 1. You just have to figure out how many iterations are needed. For n = 8 and a 32-bit integer, 4 iterations should be enough.

    EDIT 2:

    Here’s a slightly more optimized version combined with Paul R.’s conditional subtract code:

    unsigned int x = 4023156861;
    
    x = (x & 65535) + (x >> 16);     //  Reduce to 17 bits
    x = (x & 255) + (x >> 8);        //  Reduce to 9 bits
    x = (x & 255) + (x >> 8);        //  Reduce to 8 bits
    x = (x + ((x + 1) >> 8)) & 255;  //  Reduce to < 255
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to terminate a while() loop with an unsigned int? For example
Unsigned int into a short and back. Is this possible? How to do it
I have an unsigned int variable and it can only have the values of
warning C4244: '=' : conversion from 'unsigned int' to 'float', possible loss of data
Is it possible that converting from size_t to unsigned int result in overflow .
Possible Duplicate: Using Recursion to raise a base to its exponent - C++ int
Possible Duplicate: What does template <unsigned int N> mean? Hi ! Are non-type template
For example, I have the following table: CREATE TABLE `test` ( `total_results` int(10) unsigned
unsigned int *pMessageLength, MessageLength; char *pszParsePos; ... //DATA into pszParsePos ... printf(\nMessage Length\nb1: %d\nb2:
Given: unsigned int a, b, c, d; I want: d = a * b

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.