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

The Archive Base Latest Questions

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

This program below moves the last (junior) and the penultimate bytes variable i type

  • 0

This program below moves the last (junior) and the penultimate bytes variable i type int. I’m trying to understand why the programmer wrote this

i = (i & LEADING_TWO_BYTES_MASK) | ((i & PENULTIMATE_BYTE_MASK) >> 8) | ((i & LAST_BYTE_MASK) << 8);

Can anyone explain to me in plain English whats going on in the program below.

#include <stdio.h>
#include <cstdlib>

#define LAST_BYTE_MASK 255 //11111111
#define PENULTIMATE_BYTE_MASK 65280 //1111111100000000
#define LEADING_TWO_BYTES_MASK 4294901760 //11111111111111110000000000000000


int main(){
    unsigned int i = 0;
    printf("i = ");
    scanf("%d", &i);
    i = (i & LEADING_TWO_BYTES_MASK) | ((i & PENULTIMATE_BYTE_MASK) >> 8) | ((i & LAST_BYTE_MASK) << 8);
    printf("i = %d", i);
    system("pause");
}
  • 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-31T18:11:04+00:00Added an answer on May 31, 2026 at 6:11 pm

    The expression is indeed a bit convoluted but in essence the author does this:

    // Mask out relevant bytes
    unsigned higher_order_bytes = i & LEADING_TWO_BYTES_MASK;
    unsigned first_byte = i & LAST_BYTE_MASK;
    unsigned second_byte = i & PENULTIMATE_BYTE_MASK;
    
    // Switch positions:
    unsigned first_to_second = first_byte << 8;
    unsigned second_to_first = second_byte >> 8;
    
    // Concatenate back together:
    unsigned result = higher_order_bytes | first_to_second | second_to_first;
    

    Incidentally, defining the masks using hexadecimal notation is more readable than using decimal. Furthermore, using #define here is misguided. Both C and C++ have const:

    unsigned const LEADING_TWO_BYTES_MASK = 0xFFFF0000;
    unsigned const PENULTIMATE_BYTE_MASK = 0xFF00;
    unsigned const LAST_BYTE_MASK = 0xFF;
    

    To understand this code you need to know what &, | and bit shifts are doing on the bit level.

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

Sidebar

Related Questions

Below is the program that I wrote. /******************************************************************************* * This program reads EOF from
I am trying to understand the output of the program printed below. When I
In this below program i am printing the contents of the div when i
This code below doesn't work correctly since my MFC program is in unicode circumstance.
This is with reference to the below question: Execute program from within a C
I'm working on a picture select/crop program and using this code below: http://www.androidworks.com/crop_large_photos_with_android/comment-page-1#comment-969 As
So this problem is a little bit complicated to understand what I'm trying to
This program I use has it's own variables to set when you run it,
This program stores pairs in a map, counting the number of times a word
This program reads emails (really just a .txt file structured like an email) and

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.