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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:25:04+00:00 2026-05-17T02:25:04+00:00

I want to read 4 bytes which are a little-endian encoding of an unsigned

  • 0

I want to read 4 bytes which are a little-endian encoding of an unsigned 32-bit integer, and assign the value to a Java int

(Yes, in reality I will use a ‘long’, but in this case I ‘know’ that the unsigned value is never so big that it will overflow a signed int in two’s complement notation, and it suits my illustration to use an int).

The 4 bytes in question encode the value ‘216’ little-endian style:

0xD8000000

And basically I just need to stuff the following bit pattern into Java int:

0x000000D8

The following simple code ought to do it … and for the first three ‘0x00’ bytes it succeeds:

byte b1 = din.readByte();
byte b2 = din.readByte();
byte b3 = din.readByte();
byte b4 = din.readByte();
int s = 0;
s = s | b4;
s = (s << 8);
s = s | b3;
s = (s << 8);
s = s | b2;
s = (s << 8);
s = s | b1;
return s;

However, it screws up on:

s = s | b1;

… because the bits of b1 are 1101 1000, which is a negative number (-40) in two’s complement notation, since the most-significant bit is a 1. When Java widens b1 to an int before evaluating the bitwise or operator |, -40 is encoded as 0xFFFFFFD8, which screws our naive assumption that the first 3 bytes of the widened int will be 0’s.

So my strategy runs aground. But what should I do instead? Is it even possible to solve this using primitive operators (please give solution), or must we resort to the class library? (I don’t play around with bits and bytes directly very much in my normal coding, so I lack the idiom for what seems like it ought to be ‘everyday’ code).

As for the class library approach, the following fragment gets the right result:

ByteBuffer b = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).put((byte) 0xD8).put((byte) 0x00).put((byte) 0x00).put((byte) 0x00);
b.flip();
int s = b.getInt();

… which is fine for readability, but uses 8 method invocations which I’d rather dispense with.

thanks!
David.

  • 1 1 Answer
  • 1 View
  • 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-17T02:25:05+00:00Added an answer on May 17, 2026 at 2:25 am

    Just include & 0xff for each byte to int promotion, in order to make sure that the top bits are set to 0:

    byte b1 = din.readByte();
    byte b2 = din.readByte();
    byte b3 = din.readByte();
    byte b4 = din.readByte();
    int s = 0;
    s = s | (b4 & 0xff);
    s = (s << 8);
    s = s | (b3 & 0xff);
    s = (s << 8);
    s = s | (b2 & 0xff);
    s = (s << 8);
    s = s | (b1 & 0xff);
    return s;
    

    Or more compactly:

    byte b1 = din.readByte();
    byte b2 = din.readByte();
    byte b3 = din.readByte();
    byte b4 = din.readByte();
    return ((b4 & 0xff) << 24)
         | ((b3 & 0xff) << 16)
         | ((b2 & 0xff) << 8)
         | ((b1 & 0xff) << 0);
    

    (Obviously the “shift left 0” is unnecessary, but it keeps the consistency higher.)

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

Sidebar

Related Questions

I have a huge binary file from which I want to read some bytes
I want to be able to read bytes from a min and max hexadecimal
I have a multi-byte primitive type called s32 which I want to read from
I want to read a sequence of bytes from my accelerometer. I can't get
I am writing code that compares 2 bytes which represent integers. I want to
i want to read\write a binary file which has the following structure: The file
i try to read file of bytes written in hex for example: (909090) which
I have a 256-bit value in Verilog: reg [255:0] val; I want to define
I want to read a raw file, which has 3 interleaving and has a
Hi I can't understand this error I want read this XML file: <?xml version=1.0

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.