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

  • Home
  • SEARCH
  • 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 9228413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:19:05+00:00 2026-06-18T05:19:05+00:00

I have an assignment that asks to do a number of functions only using

  • 0

I have an assignment that asks to do a number of functions only using these operators:

! ~ & ^ | + << >>

In some of the problems it is useful to make some integer, x, become all 1s if it contains any 1s but stay 0 if it is 0. The reason I do this is so I can return y or z like this:

// One of the two conditional values is now 0
int conditionalA = mask&y;
int conditionalB = ~mask&z;

// One of the values is combined with 0 using |
int out = conditionalA|conditionalB;

return out;

where I made mask like this:

// Make any x other than 0 all 1s
int mask = x;
mask |= mask>>1;
mask |= mask>>2;
mask |= mask>>4;
mask |= mask>>8;
mask |= mask>>16;

mask |= mask<<1;
mask |= mask<<2;
mask |= mask<<4;
mask |= mask<<8;
mask |= mask<<16;

There must be a better way to make mask all 1s or 0s but I can’t think of a more efficient solution. Again, it is important that 0 remains 0 if x was 0.

Edit: If statements are not an option

  • 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-18T05:19:06+00:00Added an answer on June 18, 2026 at 5:19 am

    Assuming 2’s complement:

    int mask = !x + ~0;
    

    The ! maps any nonzero value to 0 and 0 to 1, then we add ~0 (-1) to get -1 and 0 respectively.

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

Sidebar

Related Questions

I have quite a large XSL document for an assignment that does a number
For an assignment in college, we have to make a script in Perl that
I have an assignment that is making a processing sketch that make 4 by
I have an assignment that asks me to retrieve delimited text from form values.
We have assignment at school that we make an action: Everytime I click on
hey guys, i have a c++ programming assignment that asks me to create a
I have a homework assignment that asks to create an order form. The order
I'm very new to XML and XSLT. I have a homework assignment that asks
I have an assignment that wants me to write an ternary search algorithm and
I have an assignment that simulates a dice game. As part of the program,

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.