Get busy this weekend, with OR XOR AND [Excel Homework]

Some of you have heard my neighbor’s dog bark in a video or two that I recorded. While I was busy explaining how to do something awesome in Excel, this dog would decide to bark, adding her own two cents to the lesson. Quite a few of my VBA class students have grown used to it. So much that they complain when a lesson doesn’t have a couple of woofs. But I digress.

So coming back, one of the dogs (probably stray) has decided that she should bring her infant puppies and hide them under our terrace stair case. So, now we have 2 cute little puppies barking day long (and very late in to the night) just outside my office window. We have tried hard to get rid of them, but they somehow sneak back in and start barking or crying. So, I will be busy this weekend trying to move them out.

But that doesn’t mean, you have to live Excel-less for a few days. So I have a homework.

OR XOR AND, Get busy this weekend!

Don’t worry. I am not speaking elvish or something. OR, XOR & AND stand for bit-wise operations.

Binary?!? What binary?

As the saying goes, “There are 10 types of people in this world. Those who understand binary and those who don’t.”

If you understand the quotation, then you probably know what bit-wise operations are. If not, then I guess you were too busy during high-school math lessons.

We are pretty comfortable dealing with regular numbers, those that have base 10.

Binary numbers are in base 2. That means, everything is a zero or one. So 5 becomes 101 (that is 1x22+0x21+1x20 = 5)

So what are bit-wise operations?

Just as you have plus, minus, multiplication etc. there are few operations in binary world. See this illustration to understand what some of the basic bit-wise operations mean:

Understanding Bit-wise Operations in Excel

What is the homework then?

Very simple, assuming 2 cells A1 & A2 have binary numbers in them,

What formulas you would write to produce the AND, OR & XOR of the numbers?


  • How would you write formulas if your numbers are less than 512?
  • How would you write if the numbers are less than 65536?
  • What would you do if the numbers are very very large?

Bitwise Operators & Bitwise Formulas in Excel - Howto

Go ahead and post your answers

This is no ordinary exercise. But probably it will be easier than getting unwanted puppies out of your house. So go ahead and give this a try. And post your answers using comments. I am waiting.

Some Tips

That is all. I wish you a pleasant weekend then. Mine is going to be noisy for sure.


Hello Awesome...

My name is Chandoo. Thanks for dropping by. My mission is to make you awesome in Excel & your work. I live in Wellington, New Zealand. When I am not F9ing my formulas, I cycle, cook or play lego with my kids. Know more about me.

I hope you enjoyed this article. Visit Excel for Beginner or Advanced Excel pages to learn more or join my online video class to master Excel.

Thank you and see you around.

Written by Chandoo
Tags: , , , , , , ,
Home: Main Page
? Doubt: Ask an Excel Question

24 Responses to “Get busy this weekend, with OR XOR AND [Excel Homework]”

  1. Stef@n says:

    first solution for AND
    The two numbers are in A1 and B1
    = SUBSTITUTE (SUBSTITUTE (A1+B1*9*9, 9, 1), 8, 0)

  2. Stef@n says:

    next solution for OR
    =1*SUBSTITUTE (A1+A2;2;1)


  3. Stef@n says:

    last solution for XOR
    =1*SUBSTITUTE (A1+A2;2;0)


  4. Irvine says:

    Or you could make use of the VBA logical operators!

    Define the following as custom functions
    Public Function BITXOR(x As Long, y As Long)
    BITXOR = x Xor y
    End Function

    Public Function BITAND(x As Long, y As Long)
    BITAND = x And y
    End Function

    Public Function BITOR(x As Long, y As Long)
    BITOR = x Or y
    End Function

    and then use them such:
    0101 0100 0101 0100 0001

  5. Stef@n says:

    an another solution for AND
    =1*SUBSTITUTE (SUBSTITUTE (A1+A2;1;0);2;1)

    the binary numbers are in A1 and A2 !

  6. Shair says:

    I was obviously playing hooky at the beach during the bit-wise math lesson – you lost me at “Understanding bit-wise operations” 🙂

  7. Anup Agarwal says:

    After looking at the above solutions, I find my solution silly, but still:

    For the following formulae,
    Row 1: headers,
    Row 2: OR
    Row 3: AND
    Row 4: XOR

    Column 1: Input 1
    Column 2: Input 2
    Column 3: Result




  8. Hui... says:

    Please don't consider your solution silly
    Firstly, You are the 3rd person to submit an answer
    Secondly, The best formula/function is the one that you know and understand.

  9. Michael Azer says:

    I think I have a very tedious solution, which people won't have the patience to do except in small numbers.

    I used the same problem setup as "Anup Agarwal"

    AND =IF(AND(MID(B2,1,1)="1",MID(C2,1,1)="1"),1,0)&IF(AND(MID(B2,2,1)="1",MID(C2,2,1)="1"),1,0)&IF(AND(MID(B2,3,1)="1",MID(C2,3,1)="1"),1,0)&IF(AND(MID(B2,4,1)="1",MID(C2,4,1)="1"),1,0)

    OR =IF(OR(MID(B3,1,1)="1",MID(C3,1,1)="1"),1,0)&IF(OR(MID(B3,2,1)="1",MID(C3,2,1)="1"),1,0)&IF(OR(MID(B3,3,1)="1",MID(C3,3,1)="1"),1,0)&IF(OR(MID(B3,4,1)="1",MID(C3,4,1)="1"),1,0)


  10. Michael Azer says:

    Sorry my last post was totally messed up


  11. Stephen says:

    I just couldn't get your solutions to work.
    01010101010 + 01010101110 = 02020210120
    what am i doing wrong?

  12. Stephen says:

    ...I got yours to work!

  13. Rich says:

    @Stephen - I get the same, but Stef@an's second solution for AND does work (at least for the test cases I used)

  14. Stef@n says:

    @ Stephen / Rich
    yes , you are right ! - only this works:

    =1*SUBSTITUTE (A1+A2;2;1)
    =1*SUBSTITUTE (A1+A2;2;0)
    =1*SUBSTITUTE (SUBSTITUTE (A1+A2;1;0);2;1)

  15. Michael Azer says:

    @Stef@n - You're answer is really smart, I never knew about the substitute function before. Great Work!

  16. Stef@n says:

    Thx Michael 🙂
    yes - it is simply easy 😉
    if you add 1 and 1 - excel calculate 2
    and then you have to substitute the 2 - new = 0 respectively 1

  17. Rob says:

    Here is a good resource for people wanting to learn binary and hexadecimal.

  18. Kyle McGhee says:

    Three that weren't asked for:



    (was using Daniel Ferry's bitwise file to verify against)

  19. Q says:

    @ Kyle

    Not only takes one parameter and inverts 0 -1 and 1-0

    Took out the +A2

  20. Great solutions!

    I'll add two:

    NAND =1*SUBSTITUTE (A1+A2,2,0)


  21. John Fuller says:

    This will work for binary numbers of any size (although the text format mask will have to have as many zeroes as there are digits in the longest addend)

    Assume binary #s are in C35 & C36, then add and format as text in C37:
    -sum- = 101112211112

    AND - SUBSTITUTE 0s for 1s in -sum-, then sub 1s for 2s

    OR - sub 1s for 2s in -sum-

    XOR - sub 0s for 2s in -sum-

  22. Shane says:

    Just wandered by:



Leave a Reply

« »