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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:14:19+00:00 2026-05-28T05:14:19+00:00

I want a simple C function which will return true if the n-th bit

  • 0

I want a simple C function which will return true if the n-th bit in a byte is set to1. Otherwise it will return false.

This is a critical function in terms of execution time, so I am thinking of the most optimal way to do that.

  • 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-28T05:14:19+00:00Added an answer on May 28, 2026 at 5:14 am

    The following function can do what you need:

    int isNthBitSet (unsigned char c, int n) {
        static unsigned char mask[] = {128, 64, 32, 16, 8, 4, 2, 1};
        return ((c & mask[n]) != 0);
    }
    

    This assumes 8-bit bytes (not a given in C) and the zeroth bit being the highest order one. If those assumption are incorrect, it simply comes down to expanding and/or re-ordering the mask array.

    No error checking is done since you cited speed as the most important consideration. Do not pass in an invalid n, that’ll be undefined behaviour.

    At insane optimisation level -O3, gcc gives us:

    isNthBitSet:    pushl   %ebp
                    movl    %esp, %ebp
                    movl    12(%ebp), %eax
                    movzbl  8(%ebp), %edx
                    popl    %ebp
                    testb   %dl, mask(%eax)
                    setne   %al
                    movzbl  %al, %eax
                    ret
    mask:           .byte   -128, 64, 32, 16, 8, 4, 2, 1
    

    which is pretty small and efficient. And if you make it static and suggest inlining, or force it inline as a macro definition, you can even bypass the cost of a function call.

    Just make sure you benchmark any solution you’re given, including this one (a). The number one mantra in optimisation is “Measure, don’t guess!”

    If you want to know how the bitwise operators work, see here. The simplified AND-only version is below.

    The AND operation & will set a bit in the target only if both bits are set in the tewo sources. The relevant table is:

    AND | 0 1
    ----+----
     0  | 0 0
     1  | 0 1
    

    For a given char value, we use the single-bit bit masks to check if a bit is set. Let’s say you have the value 13 and you want to see if the third-from-least-significant bit is set.

    Decimal  Binary
      13     0000 1101
       4     0000 0100 (the bitmask for the third-from-least bit).
             =========
             0000 0100 (the result of the AND operation).
    

    You can see that all the zero bits in the mask result in the equivalent result bits being zero. The single one bit in the mask will basically let the equivalent bit in the value flow through to the result. The result is then zero if the bit we’re checking was zero, or non-zero if it was one.

    That’s where the expression in the return statement comes from. The values in the mask lookup table are all the single-bit masks:

    Decimal  Binary
      128    1000 0000
       64    0100 0000
       32    0010 0000
       16    0001 0000
        8    0000 1000
        4    0000 0100
        2    0000 0010
        1    0000 0001
    

    (a) I know how good I am, but you don’t 🙂

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

Sidebar

Related Questions

I want to create a simple menu function which can call it example get_menu()
I want my Makefile to be as simple as possible and still function. This
I have a simple module which will return a form , but now i
I want a simple function that receives a string and returns an array of
I have a simple function that I want to call in the code behind
I want to run a simple JavaScript function on a click without any redirection.
I am calling a function which returns a string containing XML data. How this
I have the following simple javascript code, which handles the Return Key, I don't
I wrote a simple function to create a hashtable out of an xml-file which
I have a function in php which I would like to perform a simple

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.