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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:00:26+00:00 2026-06-12T13:00:26+00:00

If we have a decimal value: 123 and its binary version: 01111011 How can

  • 0

If we have a decimal value: 123
and its binary version: 01111011

How can I get four leftmost and the four rightmost bits from this byte into 2 separate int variables?

I mean:

int a = 7;  // 0111 (the first four bits from the left)
int b = 11; // 1011 (the first four bits from the right)

Much appreciated!

  • 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-12T13:00:28+00:00Added an answer on June 12, 2026 at 1:00 pm
    int x = 123;
    int low = x & 0x0F;
    int high = (x & 0xF0) >> 4;
    

    This is called masking and shifting. By ANDing with 0xF (which is binary 00001111) we remove the higher four bits. ANDing with 0xF0 (which is binary 11110000) removes the lower four bits. Then (in the latter case), we shift to the right by 4 bits, in effect, pushing away the lower 4 bits and leaving only what were the upper 4 bits.

    As @owlstead says in the comments below, there’s another way to get the higher bits. Instead of masking the lower bits then shifting, we can just shift.

    int high = x >> 4;

    Note that we don’t need to mask the lower bits since whatever they were, they’re gone (we’ve pushed them out). The above example is clearer since we explicitly zero them out first, but there’s no need to do so for this particular example.

    But to deal with numbers bigger than 16 bits (int is usually 32 bits), we still need to mask, because we can have the even higher sixteen bits getting in the way!

    int high = (x >> 4) & 0x0F;

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

Sidebar

Related Questions

I have a decimal value of a utf-32 character: $a=21644; How can I convert
I have a price textbox and I want to get a decimal value with
Possible Duplicate: C# Parse a Number from Exponential Notation I currently have: decimal value
I have a textbox that gets a decimal value say 10500.00 the problem is
I have been successful at rounding my value to one decimal point, but I
Possible Duplicate: C# How to remove 0 from the end of a decimal value
I need to have objects sorted by price (decimal) value for fast access. I
I have a varchar column that has generally has a decimal value, but some
I have an editable cell which contain decimal value for example 2.123456 (6 place
I have a decimal value (64.17 hours) and need to convert this to 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.