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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:51:41+00:00 2026-05-31T17:51:41+00:00

Yesterday,in an Interview I have been asked to test the 5th bit in a

  • 0

Yesterday,in an Interview I have been asked to test the 5th bit in a number(to test whether its on and off)and below is how I done it.

int number = 16;
int mask   = 1<<5;

if ((number & mask) == 0)
    printf("Bit is off");
else
    printf("its on");

but he then asked me to optimize this code and do it without using this particular mask.

So my questions what else mask I could have used in this code?

  • 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-31T17:51:43+00:00Added an answer on May 31, 2026 at 5:51 pm

    Maybe the interviewer wanted to see how you reacted to a simple challenge. Or simply wanted to know if you really understood C, and would stand your ground? Maybe the interviewer wanted to know if you knew non-zero is true, and hence test your depth of understanding of C? Or maybe whether you could do binary to hex in your head?

    IMHO interviews are about a lot more than code. They are expensive to do. I always tried to get a clear impression of the person, something hard to do by written communication, or even on the phone. After all, some of those folks are going to work with the recruit!

    The most compact, and possibly the quickest is probably:

    int number = 16;  // is this really what they gave?
    
    printf((number & 0x20)?"its on":"Bit is off"); // did they mean 5th or bit 5?
    

    Edit:

    I’ve coded up the original approach, and my alternative, and compiled it for ARM Coretx-M3/4 (this is the processor I am writing for at the moment). It was compiled with -O3. Then I have disassembled each compiled file (using objdump) to get the assembler. I did it this way because the output of gcc -S was huge; that includes a lot of extra information for the assembler and linker, which made it harder to find the code.

    I replaced printf with a dummy_printf to avoid #including stdio.h which added more noise. The dummy_printf isn’t exactly the same as printf, it just takes one parameter, but it keeps the output short 🙂

    The source (all 7 files appended to make it easier to read) are at:
    http://pastebin.com/PTeApu9n

    The resulting concatenated output of objdump (for each .o) is at:
    http://pastebin.com/kHAmakE3

    As you can see, the original is compiled to:

    void original_bit5(int number) {
        int mask = 1<<5;
    
        if ((number & mask) == 0)
       0:   f010 0f20   tst.w   r0, #32
       4:   d005        beq.n   1a <original_bit5+0x1a>
            dummy_printf("Bit is off");
        else
            dummy_printf("its on"); 
       6:   f240 0000   movw    r0, #0
       a:   f2c0 0000   movt    r0, #0
       e:   f7ff bffe   b.w 0 <dummy_printf>
    
    void original_bit5(int number) {
        int mask = 1<<5;
    
        if ((number & mask) == 0)
            dummy_printf("Bit is off");
      12:   f240 0000   movw    r0, #0
      16:   f2c0 0000   movt    r0, #0
      1a:   f7ff bffe   b.w 0 <dummy_printf>
      1e:   bf00        nop
    

    I think the call to dummy_printf is using tail-call chaining, i.e. dummy_printf will not return to this function. Very efficient!

    There is no function entry code because the first four function parameters are passed in registers r0-r3.

    You can’t see the addresses of the two strings being loaded in r0. That is because this hasn’t been linked.

    You can see that:

    int mask = 1<<5;    
    if ((number & mask) == 0)
    

    is compiled to:

       0:   f010 0f20   tst.w   r0, #32
       4:   d005        beq.n   1a <original_bit5+0x1a>
    

    So 1<<5 and (... == 0), are compiler to a more direct and efficient sequence of instructions. There is a branch to the appropriate call of dummy_printf.

    My code compiles to:

    void my_bit5(int number) {
        dummy_printf((number & 0x20)?"its on":"Bit is off");    
       0:   f240 0200   movw    r2, #0
       4:   f240 0300   movw    r3, #0
       8:   f010 0f20   tst.w   r0, #32
       c:   f2c0 0200   movt    r2, #0
      10:   f2c0 0300   movt    r3, #0
      14:   bf14        ite ne
      16:   4610        movne   r0, r2
      18:   4618        moveq   r0, r3
      1a:   f7ff bffe   b.w 0 <dummy_printf>
      1e:   bf00        nop
    

    This also seems to get tail-call optimised, i.e. there is no return from this function because there is no need of one, the return by dummy_printf will return directly to main()

    What you can’t see is the two registers, r2 and r2 will contain the addresses of the two strings. That is because this hasn’t been linked.

    As you can see there is a conditional execution instruction ‘ite’ which loads the parameter register r0 with either register r2 or register r3. So there is no branch in this code.

    For a simple processor with pipelining, this can be quite efficient. On a simple pipelined processor, a branch can cause a ‘pipeline ‘stall’ while parts of the pipeline are cleared out. This varies from processor to processor. So I assume gcc has got it right, and generated a better code sequence than executing a branch. I haven’t checked.

    Spurred on by Lundin, I have come up with this:

    void union_bit5(int number) {
        union { int n; struct { unsigned :5; unsigned bit :1; }; } tester;
        tester.n = number;
    
        if (tester.bit)
            dummy_printf("Bit is on");
        else
            dummy_printf("its off");    
    }
    

    It does not explicitly include a mask, or bit shifting. It is almost certainly compiler dependent, you’d have to test it to ensure it works (glerk !-(

    gcc for ARM generates the same code (bne vs beq, but that can be adjusted) as the OP’s solution, so no optimisation, but it removes the mask:

    void union_bit5(int number) {
        union { int n; struct { unsigned :5; unsigned bit :1; }; } tester;
        tester.n = number;
    
        if (tester.bit)
       0:   f010 0f20   tst.w   r0, #32
       4:   d105        bne.n   1a <union_bit5+0x1a>
            dummy_printf("Bit is on");
        else
            dummy_printf("its off");    
       6:   f240 0000   movw    r0, #0
       a:   f2c0 0000   movt    r0, #0
       e:   f7ff bffe   b.w 0 <dummy_printf>
    void union_bit5(int number) {
        union { int n; struct { unsigned :5; unsigned bit :1; }; } tester;
        tester.n = number;
    
        if (tester.bit)
            dummy_printf("Bit is on");
      12:   f240 0000   movw    r0, #0
      16:   f2c0 0000   movt    r0, #0
      1a:   f7ff bffe   b.w 0 <dummy_printf>
      1e:   bf00        nop
    

    For what it’s worth:

    (number & 0x20)? dummy_printf("its on") : dummy_printf("Bit is off");
    

    gcc for ARM generates exactly the same code as the OP’s. It generates branch, and not conditional instructions.

    Summary:

    1. The original code is compiled to a very efficient sequence of instructions
    2. The ternary ...?...:... operator can compile to code which does not involve branches on the ARM Cortex-M3/4, but may also generate normal branch instructions.
    3. It is difficult to write more efficient code than the original in this case 🙂

    I’ll add, IMHO the cost of doing a printf is so enormous compared to a bit test, that worrying about optimising a bit test is too small an issue; it fails Amdahl’s Law. An appropriate tactic for the bit test is to ensure -O3 or -Os is used.


    If you wanted to to do something somewhat perverse (especially for such a trivial problem), but different, which might make the interviewer think, create a lookup table for every byte value. (I don’t expect it to be faster …)

    #define BIT5(val) (((val)&0x20)?1:0)
    const unsigned char bit5[256] = {
    BIT5(0x00),BIT5(0x01), BIT5(0x02),BIT5(0x03), 
    BIT5(0x04),BIT5(0x05), BIT5(0x06),BIT5(0x07),
    // ... you get the idea ...
    BIT5(0xF8),BIT5(0xF9), BIT5(0xFA),BIT5(0xFB), 
    BIT5(0xFC),BIT5(0xFD), BIT5(0xFE),BIT5(0xFF)
    };
    
    //...
    if (bit5[(unsigned char)number]) {
        printf("its on");
    } else {
        printf("Bit is off");
    }
    

    This is a handy technique if there are some complex bit patterns in, for example, a peripheral register, which need converting to a decision, or switch. It is O(1) too

    You could combine the two!-)

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

Sidebar

Related Questions

I have the below 2 Problems asked yesterday in an interview 1> Given a
Yesterday in my interview I was asked this question. (At that time I was
I had telephone interview question yesterday. The interviewer asked me if I had faced
this is an interview question my friend was asked yesterday. The question was something
Yesterday I have been testing one single query for over hours and hours, but
I was asked the output of the following code in my interview yesterday #include
Yesterday on an interview the interviewer asked me a question: Why doesn't the following
so i took the codility interview test yesterday and was informed today that i
Was asked in an interview yesterday to name three good and three bad things
I was asked the following question in my interview yesterday: Consider a Java or

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.