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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:25:10+00:00 2026-06-12T20:25:10+00:00

Since C’s bitwise operators & , | , and ~ are typically also assembly

  • 0

Since C’s bitwise operators &, |, and ~ are typically also assembly language opcodes as well, bit masking code should in principle be very quick.

I’ve got some choices of how I add bit-masking inside the inner loop of a simulation algorithm. Essentially it boils down to the choice between using an array of pre-canned masks, or changing masks dynamically using left and right shifts.

Are there particular tricks / techniques to keep bit-masking as free from unnecessary overhead as possible? Are any of the three approaches below particularly good / bad from an efficieny point of view?

  • Option 1: looping through an array of pre-canned masks, e.g. picking off particular bits

    unsigned char mask[8]={0x80,0x40,0x20,0x10,0x8,0x4,0x2,0x1};
    for(i=0;i<8;i++) {
        ...
        (mask[i] & mem_data )
        ...
    

    }

  • Option 2: down-shifting more than one place on each loop iteration

    unsigned char mask=0x80;
    for(i=0;i<8;i++) {
         ...
         mem_data & (mask>>i)
         ...
    }
    
  • Option 3: down-shifting exactly one place on each loop iteration

    unsigned char mask=0x80;
    while(mask) {
          ...
          mem_data & mask
          mask>>=1;
          ...
    }
    

Edit: Removed putchar() from the examples so it does not distract from the question

  • 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-12T20:25:11+00:00Added an answer on June 12, 2026 at 8:25 pm

    Bit masks are often used in the embedded world.

    Not all processors can do a bitwise shift with a variable argument. For example, on MSP430 processors you can only bitshift one bit at a time. The implementation will resort to software to shift with a variable amount. In that case the option number 2 has to be avoided. More generally look at the assembly output of your program to compare the most efficient solution.

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

Sidebar

Related Questions

since memcpy should be highly optimized nowadays, does it still make sense to optimize
Since the retina display, suddenly this piece of drawing code seems to not work
Since functions are first class objects, it should be possible to assign to the
Since my original question was a bit too vague, let me clarify. My goals
Since Graduating from a very small school in 2006 with a badly shaped &
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
Since VBScript is not available in 64-bit compiled applications, is there a way to
Since regular jQuery animations are not fluent on iOS ( .hide() , slideDown() ),
since today im facing a strange problem. When i start my app in my
Since I am developing an iOS >= 5.0 application, I am trying to change

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.