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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:10:42+00:00 2026-06-06T12:10:42+00:00

I use bitwise to turn bits on and off this way: unsigned char myChar

  • 0

I use bitwise to turn bits on and off this way:

unsigned char myChar = ...some value
myChar |= 0x01 << N // turn on the N-th bit

myChar &= ~(0x01 << N) //turn off the N-th bit

Now, suppose the value of N is know but the set/unset operation depends on the value of a bit of another unsigned char.
Since now, I’m doing this way:

if ((otherChar & (0x01 << M)) != 0)
{
    //M-th bit of otherChar is 1
    myChar |= 0x01 << N; 
}else
{
    myChar &= ~(0x01 << N);
}

This should be a sort of “moving bit” operation from an unsigned char to another.

My question:
is there any way of doing this without using the conditional? (and without std::bitset too)

  • 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-06T12:10:43+00:00Added an answer on June 6, 2026 at 12:10 pm

    The short answer is “yes”.

    The longer answer is that you use the bit directly from the source:

    unsigned char bit = 1 << N;
    
    myChar &= ~bit;             // Zero the bit without changing anything else
    myChar |= otherChar & bit;  // copy the bit from the source to the destination.
    

    This assumes you want to copy bit N from the source to bit N of the destination. If the source and destination bits might be at different offsets, things get a little more difficult. You have not only extract the correct bit from the source, but you then have to shift it to the correct place, then OR it into the destination. The basic idea is about like above, but the code for shifting is a little tedious. The problem is that you’d like to do something like:

    unsigned char temp = source & 1 << M;
    temp <<= N - M;
    dest |= temp;
    

    This will work fine if N > M, but if M > N, you end up with something like temp <<= -3;. What you’d like would be for a left shift of -3 to end up as a right shift of 3 — but that’s not what happens, so you need some conditional code to take the absolute value and figure out whether to do a right shift or left shift to get the bit from the source into the correct spot in the destination.

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

Sidebar

Related Questions

im trying to use MySQL bitwise operations for my query and i have this
What happens if you use a bitwise operator ( & , | , etc.)
I'm making a vector<bool> implementation. I save an unsigned int and use bitwise operations
I can only use bitwise operations and pointer arithmetic to solve this problem. I
I am new to Haskell and am trying to use bitwise operations from Data.Bits.
Is there any reason not to use the bitwise operators &, |, and ^
use Modern::Perl; use Algorithm::Permute; use List::AllUtils qw/uniq/; find_perms(1151); sub find_perms { my ($value) =
What I am currently know is that use bitwise and Int type to store
In a recent CODE Magazine article, John Petersen shows how to use bitwise operators
Possible Duplicate: Where would I use a bitwise operator in JavaScript? In c/c++ bitwise

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.