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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:02:13+00:00 2026-05-31T20:02:13+00:00

Hello is have a question for a school assignment i need to : Read

  • 0

Hello is have a question for a school assignment i need to :

Read a round number, and with the internal binaire code with bit 0 on the right and bit 7 on the left.

Now i need to change:
bit 0 with bit 7
bit 1 with bit 6
bit 2 with bit 5
bit 3 with bit 4

by example :

if i use hex F703 becomes F7C0
because 03 = 0000 0011 and C0 = 1100 0000
(only the right byte (8 bits) need to be switched.
The lession was about bitmanipulation but i can’t find a way to make it correct for al the 16 hexnumbers.
enter image description here

I`am puzzling for a wile now,

i am thinking for using a array for this problem or can someone say that i can be done with only bitwise ^,&,~,<<,>>, opertors ???

  • 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-31T20:02:15+00:00Added an answer on May 31, 2026 at 8:02 pm

    Study the following two functions:

    bool GetBit(int value, int bit_position)
    {
        return value & (1 << bit_position);
    }
    
    void SetBit(int& value, int bit_position, bool new_bit_value)
    {
        if (new_bit_value)
            value |= (1 << bit_position);
        else
            value &= ~(1 << bit_position);
    }
    

    So now we can read and write arbitrary bits just like an array.

    1 << N
    

    gives you:

    000...0001000...000
    

    Where the 1 is in the Nth position.

    So

    1 << 0 == 0000...0000001
    1 << 1 == 0000...0000010
    1 << 2 == 0000...0000100
    1 << 3 == 0000...0001000
    ...
    

    and so on.

    Now what happens if I BINARY AND one of the above numbers with some other number Y?

    X = 1 << N
    Z = X & Y
    

    What is Z going to look like? Well every bit apart from the Nth is definately going to be 0 isnt it? because those bits are 0 in X.

    What will the Nth bit of Z be? It depends on the value of the Nth bit of Y doesn’t it? So under what circumstances is Z zero? Precisely when the Nth bit of Y is 0. So by converting Z to a bool we can seperate out the value of the Nth bit of Y. Take another look at the GetBit function above, this is exactly what it is doing.

    Now thats reading bits, how do we set a bit? Well if we want to set a bit on we can use BINARY OR with one of the (1 << N) numbers from above:

    X = 1 << N
    Z = Y | X
    

    What is Z going to be here? Well every bit is going to be the same as Y except the Nth right? And the Nth bit is always going to be 1. So we have set the Nth bit on.

    What about setting a bit to zero? What we want to do is take a number like 11111011111 where just the Nth bit is off and then use BINARY AND. To get such a number we just use BINARY NOT:

    X = 1 << N   // 000010000
    W = ~X       // 111101111
    Z = W & Y
    

    So all the bits in Z apart from the Nth will be copies of Y. The Nth will always be off. So we have effectively set the Nth bit to 0.

    Using the above two techniques is how we have implemented SetBit.

    So now we can read and write arbitrary bits. Now we can reverse the bits of the number just like it was an array:

    int ReverseBits(int input)
    {
        int output = 0;
    
        for (int i = 0; i < N; i++)
        {
            bool bit = GetBit(input, i); // read ith bit
    
            SetBit(output, N-i-1, bit); // write (N-i-1)th bit
        }
    
        return output;
    }
    

    Please make sure you understand all this. Once you have understood this all, please close the page and implement and test them without looking at it.

    If you enjoyed this than try some of these:

    http://graphics.stanford.edu/~seander/bithacks.html

    And/or get this book:

    http://www.amazon.com/exec/obidos/ASIN/0201914654/qid%3D1033395248/sr%3D11-1/ref%3Dsr_11_1/104-7035682-9311161

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

Sidebar

Related Questions

Hello i have some question about java. here is my code: public static void
Hello I have one c++ Programming assignment question. I tried hard but somehow I
Hello guys I have a question regardless a old code a client needed a
https://stackoverflow.com/a/64983/468251 - Hello, I have question about this code, how made that working with
Hello i have a question on picking random entries from a database. I have
Hello this is may first question and I have found so far the following
Hello guys sorry if the question looks stupid to you. i have 3 tables
I have the following regexp: (?P<question>.+(?<!\[\[)) It is designed to match hello world! in
i have this: <div id=parent style=overflow:auto> ... <div id=test>Hello</div> ... </div> My question is:
Hello I have a question for a c programmer out there, we have a

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.