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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:42:31+00:00 2026-05-25T09:42:31+00:00

Using only: ! ~ & ^ | + << >> I need to find

  • 0

Using only:

! ~ & ^ | + << >>

I need to find out if a signed 32 bit integer can be represented as a 16 bit, two’s complement integer.

My first thoughts were to separate the MSB 16 bits and the LSB 16 bits and then use a mask to and the last 16 bits so if its not zero, it wont be able to be represented and then use that number to check the MSB bits.

An example of the function I need to write is: fitsInShort(33000) = 0 (cant be represented) and fitsInShort(-32768) = 1 (can be represented)

  • 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-25T09:42:31+00:00Added an answer on May 25, 2026 at 9:42 am
    bool fits16(int x)
    {
        short y = x;
        return y == x;
    }
    

    Just kidding 🙂 Here’s the real answer, assuming int is 32 bits and short is 16 bits and two’s complement represantation:

    Edit: Please see the last edit for the correct answer!

    bool fits16(int x)
    {
        /* Mask out the least significant word */
        int y = x & 0xffff0000;
        if (x & 0x00008000) {
            return y == 0xffff0000;
        } else {
            return y == 0;
        }
    }
    

    Without if statements i beleive that should do it:

    return (
        !(!(x & 0xffff0000) || !(x & 0x00008000)) ||
        !((x & 0xffff0000) || (x & 0x00008000))
    );
    

    Edit: Oli’s right. I somehow thought that they were allowed. Here’s the last attempt, with explanation:

    We need the 17 most significant bits of x to be either all ones or all zeroes. So let’s start by masking other bits out:

    int a = x & 0xffff8000; // we need a to be either 0xffff8000 or 0x00000000
    int b = a + 0x00008000; // if a == 0xffff8000 then b is now 0x00000000
                            // if a == 0x00000000 then b is now 0x00008000
                            // in any other case b has a different value
    int c = b & 0xffff7fff; // all zeroes if it fits, something else if it doesn't
    return c;
    

    Or more concisely:

    return ((x & 0xffff8000) + 0x8000) & 0xffff7fff;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to only find the cells in the table which contain &nbsp; using
Using POSIX threads & C++, I have an Insert operation which can only be
How would I go about using feature detection to find out whether I need
Using pinvoke I can find Handle of a window with particular class & name
Using only VBScript (launched from Windows Scripting Host) can I pull the DLL metadata
Using only grep and sed, is there a way I can tranform the output
Using only ANSI C, what is the best way to, with fair certainty, determine
Using only MySQL , I'm seeing if it's possible run an insert statement ONLY
Using only ANSI C, is there any way to measure time with milliseconds precision
I am looking into using only a ddl to run my query, not a

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.