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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:40:52+00:00 2026-06-10T03:40:52+00:00

How to set/unset a bit at specific position of a long in Java ?

  • 0

How to set/unset a bit at specific position of a long in Java ?

For example,

long l = 0b001100L ; // bit representation

I want to set bit at position 2 and unset bit at position 3 thus corresponding long will be,

long l = 0b001010L ; // bit representation

Can anybody help me how to do that ?

  • 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-10T03:40:53+00:00Added an answer on June 10, 2026 at 3:40 am

    To set a bit, use:

    x |= 0b1; // set LSB bit
    x |= 0b10; // set 2nd bit from LSB
    

    to erase a bit use:

    x &= ~0b1; // unset LSB bit (if set)
    x &= ~0b10; // unset 2nd bit from LSB
    

    to toggle a bit use:

    x ^= 0b1;
    

    Notice I use 0b?. You can also use any integer, eg:

    x |= 4; // sets 3rd bit
    x |= 0x4; // sets 3rd bit
    x |= 0x10; // sets 9th bit
    

    However, it makes it harder to know which bit is being changed.

    Using binary allows you to see which exact bits will be set/erased/toggled.

    To dynamically set at bit, use:

    x |= (1 << y); // set the yth bit from the LSB
    

    (1 << y) shifts the …001 y places left, so you can move the set bit y places.

    You can also set multiple bits at once:

    x |= (1 << y) | (1 << z); // set the yth and zth bit from the LSB
    

    Or to unset:

    x &= ~((1 << y) | (1 << z)); // unset yth and zth bit
    

    Or to toggle:

    x ^= (1 << y) | (1 << z); // toggle yth and zth bit
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I set and unset a cookie using jQuery, for example create a
Bitcounting can be done in several ways, eg. with set bit iterator, unset bit
I need to set/unset configure options for an in-house C# software through its GUI.
Set rs = conn.Execute(Statement) //rs has 6 fields I want to add the current
Right now I'm using this to set/unset individual bits in a byte: if (bit4Set)
If I set this for cache control on my site: Header unset Pragma FileETag
I have the following VCL: sub jamie_fetch { if(beresp.http.X-Var-Cache == YES) { unset beresp.http.Set-Cookie;
How do i unset a already set flag using fcntl? For e.g. I can
Possible Duplicate: How do you set, clear and toggle a single bit in C?
R Version 2.11.1 32-bit on Windows 7 I get a data set as below:

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.