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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:34:41+00:00 2026-06-04T09:34:41+00:00

I am looking some part of code that is supposed to get a single

  • 0

I am looking some part of code that is supposed to get a single bit from an int.
It is as follows:

private int getBit( int token, int pos){  
   return ( token & ( 1 << pos ) ) != 0 ? 1 : 0;   
}

My question is why doesn’t it do it the following (simpler) way?

return token & ( 1 << pos );   

I expect that it will also return a 0 or 1.
Am I wrong on this? Is the second (mine) version wrong?

  • 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-04T09:34:42+00:00Added an answer on June 4, 2026 at 9:34 am

    Your version is wrong. When you execute

    return token & ( 1 << pos );
    

    if it is non-zero, then you get an int with every bit except the pos bit zeroed out, because that is the number on the right side of the & operator. This obviously would only be 1 if pos==0.

    This happens because the & operator simply takes the bitwise and operation between corresponding bits in the two ints. Since 1 << pos has a 1 bit in a position besides the lowest and token can presumably be any int, the result can also have a 1 bit in a position other than the lowest, making it greater than 1.

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

Sidebar

Related Questions

Looking at some c# code from open and closed source project i see that
I have some code that I'm using to get data from a network socket.
I'm looking at how to import some third part code into a git repository.
I am looking to develop some code that will be able to by looking
Looking on some source code I've inherited, there's a snippet of code that calls
I'm fiddling with a bit of c++/opencv. I was looking some of the samples
I'm looking at some code with this form: 1 package com.stackoverflow.java.questions; 2 import java.util.ArrayList;
I was looking through some code in a string escape library the other day
I have some code that runs fairly well, but I would like to make
I am searching for a lib or tool or even some simple code that

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.