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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:50:23+00:00 2026-05-20T09:50:23+00:00

If I have to handle values to be stored in bytes like 0x118, how

  • 0

If I have to handle values to be stored in bytes like 0x118, how do I split the LSB and MSB?

I was trying the following way… I don’t think that it’s the right way:

value = 0x118;  

Storing in bytes…

result[5] = (byte) value;  
result[6] = (byte)(value << 8);  
...

What is the correct way?

  • 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-20T09:50:24+00:00Added an answer on May 20, 2026 at 9:50 am

    This will do it:

    result[5] = (byte) (value & 0xFF);           // Least significant "byte"
    result[6] = (byte) ((value & 0xFF00) >> 8);  // Most significant "byte"
    

    I usually use bit masks – maybe they’re not needed. The first line selects the lower eight bits, the second line selects the upper eight bits and shifts the bits eight bit positions to the right. This is equal to a division by 28.


    This is the “trick” behind:

      (I) LSB
    
      01010101 10101010        // Input
    & 00000000 11111111        // First mask, 0x00FF
      -----------------
      00000000 10101010        // Result - now cast to byte
    
      (II) MSB
    
      01010101 10101010        // Input
    & 11111111 00000000        // Second mask, 0xFF00
      -----------------
      01010101 00000000        // Result - 
      >>>>>>>>                 // "Shift" operation, eight positions to the right
      -----------------
      00000000 01010101        // Result - now cast to byte
    

    To sum it up, do the following calculation:

     byte msb = result[6];
     byte lsb = result[5];
     int result = (msb << 8) + lsb;    // Shift the MSB bits eight positions to the left.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's the correct way to handle two distinct values being stored in one byte
I have a UIViewController subclass that I am trying to have handle the shake
What is the best way to handle the following database search scenario using asp.net
I have a stored procedure that returns two int Values. The first can not
I have a std::map<std::string, HANDLE> SampleMap which stores HANDLE objects as value. After the
i have a function to handle click of clickedcell. I want to exctract value
I have to handle some sensitive data in my application, such as passwords, credit
I have to handle the images uploaded by users and want to make thumbnails
just a general question, do you ALWAYS have to handle error? i was just
I'm working on a larger Web based Project, that probably will have to handle

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.