Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Underflow in Byte

  1. #1
    Junior Member
    Join Date
    Nov 2019
    Posts
    22
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Underflow in Byte

    Hello,

    In the below code, we have underflow or overflow?
    Byte in java is 1 byte. It means the capacity of a byte is 0-255.
    400 - 256 (0 to 127 and -1 to -127). I expected we have 144. But we have -112 in output.
    Could you please explain it?
    Thanks

    import java.lang.System;
    import java.util.Scanner;
     
    class test
    {
     
     
        public static void main(String[] args) 
        {
            {
     
               byte b = (byte)400;
               System.out.println(b);
     
        }
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Underflow in Byte

    Use the Integer class's toHexString to see what the bits are for the value 400 = x190 or toBinaryString gives 1 1001 0000
    The sign bit for the low order byte is 1 making it negative.
    Casting it to a byte throws away the leading 1 leaving 1001 0000
    Display the binary value of -112 gives 111111111111111111111111 1001 0000
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    shervan360 (November 14th, 2019)

  4. #3
    Junior Member
    Join Date
    Nov 2019
    Posts
    22
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Underflow in Byte

    Quote Originally Posted by Norm View Post
    Use the Integer class's toHexString to see what the bits are for the value 400 = x190 or toBinaryString gives 1 1001 0000
    The sign bit for the low order byte is 1 making it negative.
    Casting it to a byte throws away the leading 1 leaving 1001 0000
    Display the binary value of -112 gives 111111111111111111111111 1001 0000
    Thank you for your answer, but I didn't understand well.
    Could you please more explain about it?

    The sign bit for the low order byte is 1 making it negative.
    Casting it to a byte throws away the leading 1s leaving 1001 0000
    Display the binary value of -112 gives 111111111111111111111111 1001 0000

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Underflow in Byte

    A byte has 8 bits. If this binary int value with these 32 bits (111111111111111111111111 1001 0000) is placed in a byte, the resulting 8 bits in the byte would be: 1001 0000.
    The high order 24 bits would be truncated.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [B instead of byte[]
    By rfabbc in forum Android Development
    Replies: 0
    Last Post: February 21st, 2013, 08:21 PM
  2. Byte Operation
    By tcstcs in forum Java Theory & Questions
    Replies: 4
    Last Post: March 27th, 2011, 11:19 PM
  3. I/O and byte arrays.
    By LDM91 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 1st, 2011, 08:36 PM
  4. UTF - 8 / Byte Stream
    By JavaCODER in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: October 16th, 2010, 02:26 PM
  5. byte[] from vector
    By perlWhite in forum Collections and Generics
    Replies: 1
    Last Post: August 26th, 2009, 05:10 AM