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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:33:53+00:00 2026-05-16T04:33:53+00:00

I have been reading about bit operators in Objective-C in Kochan’s book, Programming in

  • 0

I have been reading about bit operators in Objective-C in Kochan’s book, “Programming in Objective-C”.

I am VERY confused about this part, although I have really understood most everything else presented to me thus far.

Here is a quote from the book:

The Bitwise AND Operator

Bitwise ANDing is frequently used for masking operations. That is, this operator can be used easily to set specific bits of a data item to 0. For example, the statement

w3 = w1 & 3;

assigns to w3 the value of w1 bitwise ANDed with the constant 3. This has the same ffect of setting all the bits in w, other than the rightmost two bits to 0 and preserving the rightmost two bits from w1.

As with all binary arithmetic operators in C, the binary bit operators can also be used as assignment operators by adding an equal sign. The statement

word &= 15;

therefore performs the same function as the following:

word = word & 15;

Additionally, it has the effect of setting all but the rightmost four bits of word to 0. When using constants in performing bitwise operations, it is usually more convenient to express the constants in either octal or hexadecimal notation.

OK, so that is what I’m trying to understand. Now, I’m extremely confused with pretty much this entire concept and I am just looking for a little clarification if anyone is willing to help me out on that.

When the book references “setting all the bits” now, all of the bits.. What exactly is a bit. Isn’t that just a 0 or 1 in 2nd base, in other words, binary?

If so, why, in the first example, are all of the bits except the “rightmost 2” to 0? Is it 2 because it’s 3 – 1, taking 3 from our constant?

Thanks!

  • 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-16T04:33:53+00:00Added an answer on May 16, 2026 at 4:33 am

    Numbers can be expressed in binary like this:

    3    = 000011
    5    = 000101
    10   = 001010
    

    …etc. I’m going to assume you’re familiar with binary.

    Bitwise AND means to take two numbers, line them up on top of each other, and create a new number that has a 1 where both numbers have a 1 (everything else is 0).

    For example:

        3          =>  00011
      & 5          =>  00101
    ------           -------
        1              00001
    

    Bitwise OR means to take two numbers, line them up on top of each other, and create a new number that has a 1 where either number has a 1 (everything else is 0).

    For example:

        3          =>  00011
      | 5          =>  00101
    ------           -------
        7              00111
    

    Bitwise XOR (exclusive OR) means to take two numbers, line them up on top of each other, and create a new number that has a 1 where either number has a 1 AND the other number has a 0 (everything else is 0).

    For example:

        3          =>  00011
      ^ 5          =>  00101
    ------           -------
        6              00110  
    

    Bitwise NOR (Not OR) means to take the Bitwise OR of two numbers, and then reverse everything (where there was a 0, there’s now a 1, where there was a 1, there’s now a 0).

    Bitwise NAND (Not AND) means to take the Bitwise AND of two numbers, and then reverse everything (where there was a 0, there’s now a 1, where there was a 1, there’s now a 0).

    Continuing: why does word &= 15 set all but the 4 rightmost bits to 0? You should be able to figure it out now…

         n          =>  abcdefghjikl
      & 15          =>  000000001111
    ------            --------------
         ?              00000000jikl
    

    (0 AND a = 0, 0 AND b = 0, … j AND 1 = j, i AND 1 = i, …)

    How is this useful? In many languages, we use things called “bitmasks”. A bitmask is essentially a number that represents a whole bunch of smaller numbers combined together. We can combine numbers together using OR, and pull them apart using AND. For example:

    int MagicMap = 1;
    int MagicWand = 2;
    int MagicHat = 4;
    

    If I only have the map and the hat, I can express that as myInventoryBitmask = (MagicMap | MagicHat) and the result is my bitmask. If I don’t have anything, then my bitmask is 0. If I want to see if I have my wand, then I can do:

    int hasWand = (myInventoryBitmask & MagicWand);
    if (hasWand > 0) {
      printf("I have a wand\n");
    } else {
      printf("I don't have a wand\n");
    }
    

    Get it?

    EDIT: more stuff

    You’ll also come across the “bitshift” operator: << and >>. This just means “shift everything left n bits” or “shift everything right n bits”.

    In other words:

    1 << 3 = 0001 << 3 = 0001000 = 8

    And:

    8 >> 2 = 01000 >> 2 = 010 = 2

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

Sidebar

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.