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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:02:24+00:00 2026-05-25T19:02:24+00:00

For this assignment I can only use basic bitwise operators and no control structures,

  • 0

For this assignment I can only use basic bitwise operators and no control structures, so I’ve come up with this code so far to convert sign-magnitude to two’s complement.

int sm2tc(int x) {
    // Invert and add 1
    // Problem: sm has 2 zeros.. 1000...000 & 0000...000
    int tmin = 1 << 31;
    int mask = x >> 31;  // Determine sign of x
    int mask2 = ~tmin; // Negate tmin to get 0111111...
    int first = (x ^ mask) + (~mask + 1) ;
    int second = first & mask2; // Turns of MSB
    return second;
}

Where have I gone wrong?

  • 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-25T19:02:25+00:00Added an answer on May 25, 2026 at 7:02 pm

    So, what you really want to compute is

    result = (x & sign_bit) ? -(x & ~sign_bit) : x;
    

    But of course you’re not allowed control structures. The first step is to rewrite -(x & ~sign_bit) using just the + and ^ operators: (-1 ^ (x & ~sign_bit)) - -1. Now note that if (x & sign_bit) is zero then (0 ^ (x & ~sign_bit)) - 0 is equal to x. We now have

    result = (x & sign_bit) ? (-1 ^ (x & ~sign_bit)) - -1 : (0 ^ (x & ~sign_bit)) - 0
    

    You then just need to replace the -1 and 0 with functions of x that generate those values depending on the sign bit, and lo and behold both sides of the condition become the same expression and the condition becomes unnecessary.

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

Sidebar

Related Questions

I'm doing a basic homework assignment which looks like this: While input <> -1
Given this code: #!/usr/bin/perl -w use strict; use warnings; sub foo { return wantarray
This is about a school assignment so I'm trying to do things by the
Disclaimer: This is for a homework assignment, but the question is not regarding the
This is a bit of a long shot, but if anyone can figure it
This past summer I was developing a basic ASP.NET/SQL Server CRUD app, and unit
I am a student this is my LAST homework assignment : ) /* **(4.9)
Here is my code: /* Scott Landau Robot Lab Assignment 1 */ // Standard
This is beyond both making sense and my control. That being said here is
I want to use a temp directory that will be unique to this build.

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.