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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:11:23+00:00 2026-06-13T14:11:23+00:00

The code below displays a number stored as a Java byte value as hex,

  • 0

The code below displays a number stored as a Java byte value as hex, and in signed and unsigned representation. That might not be the correct wording. What I mean is, these numbers could be C style 1 byte unsigned char values. If I am interpreting these values (just so happens to be Java here but could be using any signed type in any language) then if the ‘number’ overflows into using the sign bit then the number will appear negative. But I can print as it would appear in unsigned interpretation by ANDing with 0xFF.

I understand how AND works but I can’t understand how the ANDing presents as unsigned. Can someone please explain?

public class understand_casting {
    public static String printbinary(byte val) {
      int displayMask = 1 << 7;
          String str = new String();
          for(int bit = 1; bit <= 8; ++bit) {
              str += (val & displayMask) == 0 ? '0' : '1';
              val <<= 1; //shift one to left
          }
          return str;
    }

  public static void main(String[] args) {   
     System.out.printf("0x50=%s, as int=%d, as unsigned int=%d\n", printbinary((byte)0x50), (byte)0x50, ((byte)0x50 & 0xFF));
     System.out.printf("0x88=%s, as int=%d, as unsigned int=%d\n", printbinary((byte)0x88),(byte)0x88, ((byte)0x88 & 0xFF));
     System.out.printf("0x3E=%s, as int=%d, as unsigned int=%d\n", printbinary((byte)0x3E),(byte)0x3E, ((byte)0x3E & 0xFF));
     System.out.printf("0xB7=%s, as int=%d, as unsigned int=%d\n", printbinary((byte)0xB7), (byte)0xB7, ((byte)0xB7 & 0xFF));   
  }
}

I get this output:

0x50=01010000, as int=80, as unsigned int=80
0x88=10001000, as int=-120, as unsigned int=136
0x3E=00111110, as int=62, as unsigned int=62
0xB7=10110111, as int=-73, as unsigned int=183
  • 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-13T14:11:25+00:00Added an answer on June 13, 2026 at 2:11 pm

    As you might expect the binary of int 1 is

    0000 0000 0000 0000 0000 0000 0000 0001
    

    according to twos complement, you negate a number by inverting it and adding 1, so the binary of the signed int -1 in twos complement looks like

    1111 1111 1111 1111 1111 1111 1111 1111
    

    Any negative number is going to have the high bit set, while any positive number is going to have the high bit zero.

    For example, 0xff looks like and has a long string of 0’s to the left

    0000 0000 0000 0000 0000 0000 1111 1111
    

    while -0xff looks like

    1111 1111 1111 1111 1111 1111 0000 0001
    

    When you mask using & you keep only the 1 bits that are present in both, so -2 & 0xff looks like

    1111 1111 1111 1111 1111 1111 1111 1110 |   -2
    0000 0000 0000 0000 0000 0000 1111 1111 | 0xff
    
    0000 0000 0000 0000 0000 0000 1111 1110 | -2 & 0xff = 0xfe
    

    ANDing with a positive value yields a positive value because the resulting high bit is always 0.

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

Sidebar

Related Questions

The code below displays either M or F in the GridView column after evaluating
The code below first displays folders as hyperlinks, you can click into these folders
In the code below, $row['site'] is an URL. In Chrome and IE8, it displays
I'm a EE newbie. I have the code below. the poll weblog displays at
My code below displays 10 entries, but it displays it in 10 rows and
I have code below of an HTML page that includes a JS file. Function
The code below works on one page, but not another page (the place it
I would like to have a TextBox that displays a number in currency format
I have a data-entry form that displays a number of fields (mostly p:inputText )
I cannot understand why the block of code below is not passing even though

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.