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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:16:01+00:00 2026-06-16T01:16:01+00:00

There is a binary string 10001110 which I know is using 2’s complement. I

  • 0

There is a binary string 10001110 which I know is using 2’s complement. I know it will be 8 bits.

It is given to me in the form of an unsigned int which equals 142.

I then need to convert this back to 10001110

then invert all the bits and add one resulting in it equaling 01110010

and then convert this to a signed int equaling -114.

How can I do this? I’m relatively new to C and have spent ages trying to figure it out!

Essentially I want to write a function that takes the unsigned int 142 and returns the signed int -114

  • 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-16T01:16:02+00:00Added an answer on June 16, 2026 at 1:16 am

    Here’s the solution I finally came up with.

    #include <stdio.h>
    #include <stdlib.h>
    
    int getBit(int no, int bit) // get bit at certain position
    {
        bit--;
        return (no & (1 << bit)) >> bit;
    }
    
    void complementNBits(int *no, int count) // invert certain number of bits
    {
        int i;
        for(i=0; i<count; i++)
        {
            *no ^= 1 << i;
        };
    }
    
    int main() 
    {
        unsigned int unsignedNo=142;
    
        int negative = 0;   
        signed int signedNo=0;
    
        // check if MSB is set to determine if negative
    
        if (getBit(unsignedNo, 8)) // if MSB is set
        {
            negative = 1;
            // invert these 8 bits and add 1. the rest of the MSBs will already be 0
            complementNBits(&unsignedNo, 8);
            unsignedNo++;
        }
    
        signedNo = unsignedNo;
        if (negative)
        {
            signedNo = signedNo * -1;
        }
    
        printf("THE SIGNED INTEGER: %i\n", signedNo);
    
    }
    

    Thanks for the help everyone!

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

Sidebar

Related Questions

There is a php script which sends a binary string to the client application:
Is there a way to configure a binary to be installed as SUID using
Is there any function to convert binary string into binary or decimal value? If
Is there any efficient algorithms to check whether a binary string is periodic or
Can I use itoa() for converting long long int to a binary string? I
I want to convert a 64-width binary string to long, there is a static
Using Mathematica Binary String: FBCD #1 I: ImportString[FBCD, {Binary, Bit}] O: {0, 1, 0,
I want to convert 64 bit binary string to 64 bit integer (unsigned). Is
Is there any solution to find a specific binary bit value inside a string
Given either the binary or string representation of an IPv6 address and its prefix

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.