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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:14:17+00:00 2026-05-18T08:14:17+00:00

I would like to cast unsigned int (32bit) A to unsigned short int (16bit)

  • 0

I would like to cast unsigned int (32bit) A to unsigned short int (16bit) B in a following way:

  • if A <= 2^16-1 then B=A
  • if A > 2^16-1 then B=2^16-1

In other words to cast A but if it is > of maximum allowed value for 16bit to set it as max value.

How can this be achieved with bit operations or other non branching method?

  • 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-18T08:14:18+00:00Added an answer on May 18, 2026 at 8:14 am

    Find minimum of two integers without branching:

    http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax

    On some rare machines where branching
    is very expensive and no condition
    move instructions exist, the above
    expression might be faster than the
    obvious approach, r = (x < y) ? x : y,
    even though it involves two more
    instructions. (Typically, the obvious
    approach is best, though.)

    Just to kick things off, here’s a brain-dead benchmark. I’m trying to get a 50/50 mix of large and small values “at random”:

    #include <iostream>
    #include <stdint.h>
    
    int main() {
        uint32_t total = 0;
        uint32_t n = 27465;
        for (int i = 0; i < 1000*1000*500; ++i) {
            n *= 30029; // worst PRNG in the world
            uint32_t a = n & 0x1ffff;
            #ifdef EMPTY
                uint16_t b = a; // gives the wrong total, of course.
            #endif
            #ifdef NORMAL
                uint16_t b = (a > 0xffff) ? 0xffff : a;
            #endif
            #ifdef RUSLIK
                uint16_t b = (-(a >> 16) >> 16) | a;
            #endif
            #ifdef BITHACK
                uint16_t b = a ^ ((0xffff ^ a) & -(0xffff < a));
            #endif
            total += b;
        }
        std::cout << total << "\n";
    }
    

    On my compiler (gcc 4.3.4 on cygwin with -O3), NORMAL wins, followed by RUSLIK, then BITHACK, respectively 0.3, 0.5 and 0.9 seconds slower than the empty loop. Really this benchmark means nothing, I haven’t even checked the emitted code to see whether the compiler’s smart enough to outwit me somewhere. But I like ruslik’s anyway.

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

Sidebar

Related Questions

I thought a literal like 0x88 would cast to signed int . but the
I would like to convert/cast some int or bigint to varchar (in tsql). But
I would like to use code similar to the following: int letterIndex[]; LinkedList<Integer> letterList;
I would like to be able to cast a value dynamically where the type
I would like to have an explicit cast between from a string to an
I would like to check whether an object (e.g. someObject ) is assignable (cast-able)
I have a double pointer data->mono_channel, where I would like to cast all the
There are some discussions about the same question but I would like to ask
I would like to perform a cast of something like this: // Entity Data
I would like to cast org.python.core.PyObject to java.lang.Boolean. Something similar to: boolean i =

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.