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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:19:58+00:00 2026-06-15T14:19:58+00:00

I know the best way to divide a number by 2 is to move

  • 0

I know the best way to divide a number by 2 is to move one bit to the left. What do I do if I am dividing by a multiple of 2 (for example 8), do I move over by 3 bits, here are my questions:

  1. How can I do such operations in C++, bit movement?
  2. How can I access higher byte of an int and lower byte of an int, if I wanted to swap their values?

I know these operations can be done at the assembly level because we are dealing with registers, I just don’t know if we have access to such things in C++.

  • 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-15T14:19:59+00:00Added an answer on June 15, 2026 at 2:19 pm

    Accessing the higher/lower bytes of an integer, and swapping them can be done in at least two ways. Either a combination of >> and |, or with a union.

    For example something like:

    short swapped = (original<<8)|(original>>8);
    

    will give you the two bytes of a 2-byte integer swapped. If you have a larger integer (e.g. 4 bytes), some more masking and shifting will be required, if all bytes are needed in some particularly shuffled order.

    Optimizing divisions by multiples of 2 with right shift (>>) is a no-optimization. You should write readable code that gives a clear idea of what is intended. The compiler will trivially perform such micro-optimizations.

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

Sidebar

Related Questions

Does anyone know the best way to limit the number of rows returned when
Does any one know the best way to create an autocomplete combobox with Knockout
I don't know the best way to form this question, but here's the code
Does any one know the best way to deploy a resource file to the
I would like to know the best way of parsing an input where multiple
Does anyone know the best way to create a SQL Server CE (Compact 3.5)
does anyone know the best way to insert a column in a datatable at
I am interested to know the best way to develop en JS. If I
I'd like to know the best way of converting a list of domain object
I am developing an android app and I want to know the best way

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.