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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:25:04+00:00 2026-06-15T00:25:04+00:00

I am working on a very bare bones ByteBuffer for use with java 1.4.

  • 0

I am working on a very bare bones ByteBuffer for use with java 1.4. I have a small skeleton that basically just have poor implementations of put/getInt() put/getLong(). My problem is that while putInt and getInt works, the getLong() (I think it is) is not working.

When I read out the fourth byte and shift that into the long it overflows. But all my variables are long so it should not overflow.

Heres the code (please bear in mind that this is just a start):

public class ByteBuffer {

    private byte[] buffer;
    private int first = 0;
    private int last = 0;
    private int size;
    private int elements;

    public ByteBuffer(int size) {
        this.size = size;
        buffer = new byte[size];
    }

    public void putInt(int theInt) {
        for (int i = 0; i < 4; i++) {
            put((byte) (theInt >>> (8 * i)));
        }
    }

    public int getInt() {
        int theInt = 0;
        for (int i = 0; i < 4; i++) {
            theInt |= (0xFF & get()) << (8 * i);
        }
        return theInt;
    }

    public void putLong(long theLong) {
        for (int i = 0; i < 8; i++) {
            put((byte) (theLong >>> (8 * i)));
        }
    }

    public long getLong() {
        long theLong = 0L;
        for (int i = 0; i < 8; i++) {
            theLong |= (long) ((0xFF & get()) << (8 * i));
        }
        return theLong;
    }

    public void put(byte theByte) {
        buffer[last++] = theByte;
        if (last == size) {
            last = 0;
        }
        elements++;
    }

    public byte get() {
        byte theByte = buffer[first++];
        if (first == size) {
            first = 0;
        }
        elements--;
        return theByte;
    }

    public byte[] array() {
        return (byte[]) buffer.clone();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        ByteBuffer buff = new ByteBuffer(512);

        buff.putLong(9223372036854775807L);
        buff.putLong(-9223372036854775808L);

        System.out.println(9223372036854775807L);
        System.out.println(-9223372036854775808L);

        long l1 = buff.getLong();
        long l2 = buff.getLong();
        System.out.println(l1);
        System.out.println(l2);
    }

}
  • 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-15T00:25:06+00:00Added an answer on June 15, 2026 at 12:25 am

    In your getLong method, you have to cast (0xFF & get()) to a long, before you can shift it more than 32 bits. You could also use a long literal (0xFFL) instead of the int literal (0xFF).

    The reason for this is that the resulting type of an “int && byte” operation (0xFF & get()) is an int. The specification of the bit shift operations is such, that “a << b” will actually shift “b modulo 32” bits if a is an int and “b modulo 64” bits if a is a long.

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

Sidebar

Related Questions

I'm working with a very small sample project that can be downloaded from here
I have a large validation form that is working very well except for one
I have a mvc 3 razor project that on pc is working very good
I am working on a project that involves sending some very small encrypted messages
I'm currently working on trying to create a very basic bare bones instant messenger
i'm working on a small project that might have an additional coder at some
I have been working very hard on figuring this out and just can't understand
I've been working very long on trying to straighten this out, but I just
I have developed a simple location aware iPhone application which is functionally working very
When working with very large documents, would it basically overwhelm the connection and ground

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.