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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:35:25+00:00 2026-06-10T06:35:25+00:00

In the bit-shifting example shown here : unsigned long int longInt = 1234567890; unsigned

  • 0

In the bit-shifting example shown
here:

 unsigned long int longInt = 1234567890;
 unsigned char byteArray[4];

 // convert from an unsigned long int to a 4-byte array
 byteArray[0] = (int)((longInt >> 24) & 0xFF) ;
 byteArray[1] = (int)((longInt >> 16) & 0xFF) ;
 byteArray[2] = (int)((longInt >> 8) & 0XFF);
 byteArray[3] = (int)((longInt & 0XFF));

Three questions:

  1. Why is it (int) instead of (unsigned char)? I tried it with unsigned char and it seems to compile just fine.
  2. Is 0XFF necessary? Isn’t the new bit shifted-in 0 because Wikipedia says C uses logical shifting and logical shifting shifts in 0? (EDIT: at least it doesn’t seem necessary on one with >> 24?)
  3. Can’t I just do a memcpy() to copy longInt to a unsigned char buffer? Is it not so because of issue with Endianness? Is there any other reason?
  • 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-10T06:35:27+00:00Added an answer on June 10, 2026 at 6:35 am

    1.

    ((longInt >> 24) & 0xFF) expression is of type unsigned long int. With the cast to int the expression is first converted to int then to unsigned char. If you don’t cast to int the expression is not first converted to int. There are no difference in the two situations and the cast is superfluous.

    2.

    The 0xff is not necessary. The conversion to unsigned char actually performs the same.

    3.

    You can use memcpy but it is not portable because it depends on the endianness of the system. It will give different results if the system is big endian or little endian while the bitwise shift solution will give the same results.

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

Sidebar

Related Questions

I have a problem with bit shifting and unsigned longs. Here's my test code:
I'm playing with bit shifting. I'm trying to take a 32bit int, save each
Bit of a tough one to explain here, but I'll do my best. I
Bit of a weird glitch here. The tag name of a childNode is logged
Bit of a strange question here i know. but i wanted to know if
The most efficient way to code powers of two is by bit shifting of
I use the stream operator << and the bit shifting operator << in one
I have to convert a given 16 bit integer into two 8 bit integers,
Hi everybody: let me do a bit of concept mining here: I am involved
What is the best or recommended method for bit shifting a large amount of

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.