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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:40:53+00:00 2026-05-14T05:40:53+00:00

I have 1 bit in a byte (always in the lowest order position) that

  • 0

I have 1 bit in a byte (always in the lowest order position) that I’d like to invert.
ie given 00000001 I’d like to get 00000000 and with 00000000 I’d like 00000001.

I solved it like this:

bit > 0 ? 0 : 1;

I’m curious to see how else it could be done.

  • 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-14T05:40:53+00:00Added an answer on May 14, 2026 at 5:40 am

    How about:

    bit ^= 1;
    

    This simply XOR’s the first bit with 1, which toggles it.

    If you want to flip bit #N, counting from 0 on the right towards 7 on the left (for a byte), you can use this expression:

    bit ^= (1 << N);
    

    This won’t disturb any other bits, but if the value is only ever going to be 0 or 1 in decimal value (ie. all other bits are 0), then the following can be used as well:

    bit = 1 - bit;
    

    Again, if there is only going to be one bit set, you can use the same value for 1 as in the first to flip bit #N:

    bit = (1 << N) - bit;
    

    Of course, at that point you’re not actually doing bit-manipulation in the same sense.

    The expression you have is fine as well, but again will manipulate the entire value.

    Also, if you had expressed a single bit as a bool value, you could do this:

    bit = !bit;
    

    Which toggles the value.


    More of a joke:
    Of course, the “enterprisey” way would be to use a lookup table:

    byte[] bitTranslations = new byte[256];
    bitTranslations[0] = 1;
    bitTranslations[1] = 0;
    
    bit = bitTranslations[bit];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large hexidecimal (16 byte, 32 hex digits) data item that always
I have this bit of code that is receiving a series of messages: byte[]
I have a 128-byte (1024-bit) modulus (in a byte array format) and my exponent
i have bit of code that causes an underflow: var t1, t2, delta: DWORD:
I have a bit of code that basically reads an XML document using the
I have a bit of code that passes around a ton of objects and
I have a bit of html like so: <a href=#somthing id=a1><img src=something /></a> <a
Basically, I have a byte-string of data like: \x00\x00\x00\x00 \x08\x00\x00\x00 \x05\x00\x00\x00 (spaces are used
I have this bit of code that is being converted from vb6 to vb.net.
I have a need to convert an Int32 value to a 3-byte (24-bit) integer.

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.