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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:50:41+00:00 2026-05-22T21:50:41+00:00

I have a simple question – I need to write a function for my

  • 0

I have a simple question – I need to write a function for my program to change the 3rd bit of a given byte.

I wrote those lines :

public byte turnOn(Byte value)
{
    int flag = 8;
    value = (byte) (value | flag);
    return value;
}

I’m not sure if it’s the right way to do that, because I saw also this way (with which I am unfamiliar)

value = (byte) (value | (1 << 2) );

which way is better, and what does 1 << 2 means (2 means the third bit, but what is the 1 )

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-22T21:50:42+00:00Added an answer on May 22, 2026 at 9:50 pm

    1 << 2 means 1 shifted two bits to the left. Since shifting left by one bit is similar to multiplying by two, this gives 4. In binary, this is

    00000100
    

    i.e. the 3rd bit from the right is set.

    The constant 1 is used since that number only has a single bit set – the rightmost bit. After shifting left, only the 3rd bit (from the right) is set:

    00000001 original value
    00000010 after shifting left once
    00000100 after shifting left again
    

    I prefer using 1 << 2 instead of a constant like 8, as it makes it clearer which bit is being set. It also prevents you inadvertently using a constant that has multiple bits set – unless you actually want that, of course. Even then, it’s clearer in my opinion to add together several bits, for clarity:

    final int bitsToSet = (1 << 2) + (1 << 5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple question related to one-line programming. First an example: function test(a)
I have a simple question about finding a regular expression for a given language.
I have a simple question here. I need to UPDATE a row table by
I have a simple question regarding how to write an always block in a
I have one simple question about throwing exeptions in re.match function Is there a
I have a simple question. Is there a way ( using reflections I suppose
I have a simple question and wish to hear others' experiences regarding which is
I have a simple question about Java's XML API and I hope there's a
I'm new to postgreSQL and I have a simple question: I'm trying to create
I have a very simple question. I want to test whether a particular port

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.