• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Literals

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to understand, maybe I dunt understand bytes. why when correctly casted does one get the following results:

byte b = (byte) 128 = -128;
byte c= (byte) 129 = -127;
byte c= (byte) 130 = -126;
byte c= (byte) 131 = -125;

I get the pattern but I dumt understand how it is worked out.



 
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Solving these questions will be easier, once you understand the fact that
1) byte is 8 bits long and hence the range is -128 to +127 (0 is on positive side).
2) Similarly int is 32 bits long and hence the range is -65536 to 65535
3) When an int literal is out of range for byte, looping occurs in the way that
-128 - 1 = 127
-128 - 2 = 126
+127 + 1 = -128
+127 + 2 = -127

Same applies for all primitive data type castings.

Hope that makes it crystal clear.

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a byte is singend and has a total length of 8 bits.
The first bit is for the sign (0 = positive, 1 = negative) and the other 7 bits for the number.

The binary represantion for 127 is 01111111

When you add 1 to that the binary turns into 10000000 ( = -128)

The first bit turns to 1 so that the value is negative.

If you add 1 to 10000000 you get 10000001 which is -128 + 1 and results in - 127
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sunny Bhandari wrote:Solving these questions will be easier, once you understand the fact that
1) byte is 8 bits long and hence the range is -128 to +127 (0 is on positive side).
2) Similarly int is 16 bits long and hence the range is -65536 to 65535
3) When an int literal is out of range for byte, looping occurs in the way that
-128 - 1 = 127
-128 - 2 = 126
+127 + 1 = -128
+127 + 2 = -127

Same applies for all primitive data type castings.

Hope that makes it crystal clear.




Just to making a little correction int is 32 bit log or say 4 byte .
 
Sunny Bhandari
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Javin

Thanks for pointing that out
Made the change
 
This tiny ad will self destruct in five seconds.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic