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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:47:05+00:00 2026-06-13T03:47:05+00:00

I’m trying to know the states of different bits over a char Let’s say

  • 0

I’m trying to know the states of different bits over a char

Let’s say I’ve:

char a = 11 //00001011 in binary

How can I retrieve bit number 5 which is currently 0 and cast it to a bool variable? And how can I set it?

  • 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-13T03:47:07+00:00Added an answer on June 13, 2026 at 3:47 am

    As you’ve noted:

    char a = 11 // == 00001011 in binary 
    

    Since you said the bit number 5 is clear right now, let’s *define the position of the bits as you’re talking about them:

    pos       8  7  6  5  4  3  2  1
    value    [0][0][0][0][1][0][1][1]
    

    You want to test the 5th bit, that means you need a mask that accesses the fifth bit, here’s some masks:

    your number: 00001011
    0x1  mask:   00000001
    0x2  mask:   00000010
    0x4  mask:   00000100
    0x8  mask:   00001000
    0x10 mask:   00010000  <-- So that's the one we want, the 5th bit
    

    To set your bit you need a bit-wise OR (|)

    a |= 0x10; //   00001011
               // | 00010000  because 1 | anything = 1
               // ----------
               //   00011011  The value is set! 
    

    To test the bit we can use the bit-wise AND (&)

    bool something = a & 0x10; //   00001011
                               // & 00010000  because only 1 & 1 = 1
                               // ----------
                               //   00000000  something will be 0 (false) 
    

    *it was pointed out that this is a 1-based indexing as opposed to the more typical 0-based indexing. Which is true. Based on the description I chose to read the question as “bit number 5” meaning typical (human) based counting of 1, 2, 3, 4, etc out of 8-bits.

    If you’d rather use a 0-based indexing, this is no problem, just “shift” the same logic by one, the mask for the “5th” bit in 0-based indexing is 0x20

    When talking about bits it’s good to note which is the least significant bit, and 0 or 1 based to be totally clear.

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

Sidebar

Related Questions

Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.