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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:33:03+00:00 2026-05-23T09:33:03+00:00

There is 2-bytes array: private byte[] mData; and method: public void setWord(final short pData)

  • 0

There is 2-bytes array:
private byte[] mData;

and method:

public void setWord(final short pData) {
        mData[0] = (byte) (pData >>> 8);
        mData[1] = (byte) (pData);
}

I wrote the simple test:

public void testWord() {
        Word word = new Word();
        word.setWord((short) 0x3FFF);

        Assert.assertEquals(0x3F, word.getByte(0));
        Assert.assertEquals(0xFF, word.getByte(1));
}

The second assert fails with message “Expected 255, but was -1”.
I know, that 0xFF signed short is, in fact, -1, but why JUnit thinks, that they are not equal? And, what is the correct way to implement such classes?

  • 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-23T09:33:04+00:00Added an answer on May 23, 2026 at 9:33 am

    Java does not support unsigned types, so in order for a value to be 255, it must not be a signed byte, which is incapable of holding the value of 255. The 0xFF constant value will be taken as a signed int, and for the comparison, the byte value 0xFF will be converted to an int at -1 as well.

    You need to type cast the literal 0xFF to be a byte. Change the assert to be Assert.assertEquals((byte)0xFF, word.getByte(1)); Then the left hand side will evaluate to -1 as well as the right.

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

Sidebar

Related Questions

Is there a standards-complaint method to represent a byte in ANSI (C89/90) C? I
Consider the following method (used in a factory method): private Packet(byte[] rawBytes, int startIndex)
Is there a Windows command that will output the size in bytes of a
Is there a 4 byte unsigned int data type in MS SQL Server? Am
Is there a way to change the encoding used by the String(byte[]) constructor ?
Hi is there away to detect the length of a byte before I get
I'm wondering if there is a better/inbuilt way, other than using a byte buffer
In Java, there is no such thing as an unsigned byte. Working with some
I'm using System.IO.Stream.Read(byte[] buffer, int offset, int count) . Is there an alternative to
I found the following code on the web: private byte [] StreamFile(string filename) {

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.