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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:58:53+00:00 2026-06-11T09:58:53+00:00

I am implementing a C function unsigned invert(unsigned x, int p, int n) that

  • 0

I am implementing a C function unsigned invert(unsigned x, int p, int n)
that is returning x with the n bits that begin at position p inverted, leaving the others unchanged.

#include <stdio.h>

unsigned invert(unsigned x, int p, int n);

int main()
{
    printf("%u\n", invert(11111111, 5, 4));
    printf("%u\n", invert(10, 2, 2));

    return 0;
}

unsigned invert(unsigned x, int p, int n)
{   
    return x^(~(~0<<n)<<p+1-n);
}

This is what I got so far and if I’m tracing the function through it should be right, but I keep getting 11111163 for the first test and 12 for the second test.

  • 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-11T09:58:54+00:00Added an answer on June 11, 2026 at 9:58 am

    I think your program is working. Only thing you have to do is express the number in binary.

    unsigned invert(unsigned x, int p, int n); 
    
    int main()
    {
        printf("%x\n", invert(0b11111111, 5, 4));
        printf("%x\n", invert(0b10, 2, 2));
    
    return 0;
    
    }
    
    unsigned invert(unsigned x, int p, int n)
    
    {   
         return x^(~(~0<<n)<<p+1-n);
    }
    

    Is this what you want?

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

Sidebar

Related Questions

I'm implementing a function that receives an argument which it needs to convert to
In my public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {} DBHelper function(implementing SQLite
I'm implementing a function for a homework assignment. The function definition is: int processchar(int
I am working on implementing a function that would execute another function a few
I have specific confusion in implementing function within the existing binary tree that stores
I am implementing a function that reads data from file into a multi-dimensional numpy
I'm implementing a function in C where I convert a byte[] to an int[].
I am implementing a function populatig a select box using data from another select
I am implementing a function to recursively reverse a linked-list, but getting seg-fault. typedef
I need some examples of implementing curry function in ruby(1.8.6 or 1.8.7 not 1.9).

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.