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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:59:52+00:00 2026-06-13T07:59:52+00:00

I tried to understand how if condition work with bitwise operators. A way to

  • 0

I tried to understand how if condition work with bitwise operators.
A way to check if a number is even or odd can be done by:

#include <iostream>
#include <string>
using namespace std;

string test()
{
    int i = 8;  //a number
    if(i & 1)
      return "odd";

    else
      return "even";       
}

int main ()
{
  cout << test();
  return 0;
}

The Part I don’t understand is how the if condition work. In this case if i = 8 then the in If statement it is doing 1000 & 1 which should gives back 1000 which equal 8.

If i = 7, then in if statement it should be doing 111 & 1 which gives back 111 which equal 7

Why is it the case that if(8) will return “even” and if(7) return “odd”? I guess I want to understand what the if statement is checking to be True and what to be False when dealing with bit-wise operators.

Just A thought when I wrote this question down is it because it’s actually doing

for 8: 1000 & 0001 which gives 0
for 7: 0111 & 0001 which gives 1?
  • 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-13T07:59:53+00:00Added an answer on June 13, 2026 at 7:59 am

    Yes, you are right in the last part. Binary & and | are performed bit by bit. Since

    1 & 1 == 1
    1 & 0 == 0
    0 & 1 == 0
    0 & 0 == 0
    

    we can see that:

    8 & 1 == 1000 & 0001 == 0000
    

    and

    7 & 1 == 0111 & 0001 == 0001
    

    Your test function does correctly compute whether a number is even or odd though, because a & 1 tests whether there is a 1 in the 1s place, which there only is for odd numbers.

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

Sidebar

Related Questions

I tried to execute this query and i can´t understand what´s wrong with it.
I tried to understand this line of code, but it is failed. $this->request->{self::FLAG_SHOW_CONFIG} ===
I've tried to understand some of the posts of similar, but don't quite understand
I have tried to understand AOP, Dependency Injection and Inversion of Control SPRING related
Tried examples from 'php.net' but don't understand what's the problem. Any suggestions? <?php $_SESSION['test']
I have tried my best to understand a very similar StackOverflow question , but
I have tried a lot but not been able to understand the steps involved
I understand that Aptana is built from Eclipse. I tried searching for the Package
I tried to write a program that uses threads, but couldn't understand the o/p.
I just tried dynamodb example from amazon. I understand the advantages on the db

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.