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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:50:25+00:00 2026-06-17T12:50:25+00:00

Working on a method that would take in an integer (num) and an integer

  • 0

Working on a method that would take in an integer (num) and an integer (n) that would take the absolute value of an n-bit number. I believe my logic is correct, have done it on paper and it worked out but the code seems like it is off. All help is greatly appreciated!

/**
 * Take the absolute value of an n-bit number.
 * 
 * Examples:
 *     abs(0x00001234, 16); // => 0x00001234
 *     abs(0x00001234, 13); // => 0x00000DCC
 * 
 * Note:  values passed in will only range from 1 to 31 for n.
 * 
 * @param num An n-bit 2's complement number.
 * @param n The bit length of the number.
 * @return The n-bit absolute value of num.
 */
public static int abs(int num, int n)
{
    int shifter = num << (n+1);
    int newInt = num & ~shifter;
    return newInt;

}
  • 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-06-17T12:50:27+00:00Added an answer on June 17, 2026 at 12:50 pm

    I don’t think there’s a single bitmask that will work for both positive and negative cases.

    First test if the number is negative by checking that the nth bit is 1; if not, return the original, else return the two’s complement.

    Something like this looks like it works:

    public static int abs(int num, int n)
    {
        int shifter = -1 << (n - 1);
        if ((num & shifter) == 0) 
            return num;
        shifter = shifter << 1;
        return (~num + 1) & ~shifter;
    }
    

    For example, suppose you pass in 0x1FFF as a 16 bit number, so it’s positive.
    -1 << 15 would be 0xFFFF8000 (0’s for the lowest 15 bits, 1’s for the rest), 0xFFFF8000 & 0x00001FFF is 0, and your return the original.

    If on the other hand 0x1FFF is treated as only 13 bits, then it’s negative. num & shifter will be 1 because both have the 13th bit set. Now do the two’s complement by flipping bits and adding ones. Because you’ll be flipping all 32 bits, you need to use a bitmask to zero out all the remaining ones. The original shifter works if you push it one more bit to the left and invert it.

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

Sidebar

Related Questions

I am working on a method that will allow me to pull in the
I'm working on an extension method that's only applicable to reference types. I think,
BACKGROUND I'm writing a method that will (eventually) take as input a System.Data.DataTable and
Goal: Expose a simple WCF service that would take as a parameter a string
A company that I am working for asked me take one of their websites
I have a junit test method that takes a CommonsMultipartFile object as a parameter.
I am currently working on an app that allows for free-drawing. The current method
I have created a Timer class that must call a callback method when the
I have a method that returns one of about 20 possible strings from an
I'm working with reflection read and writing objects. I have the problem that 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.