• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Java Bytes

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

Consider the below program



Now from what i understand 1 byte is 8 bits so the maximum is all 1's (ie. 255).

The above first function ByteTest1 i cast the hexadecimal a byte as so



Now why do i have to cast it even when 0x9C is well below 255 but if i do not cast it the compiler complains saying that it is an integer.

The above second function ByteTest2 i did not cast the hexadecimal a byte as so



Now why is it that the compiler accepts the hexadecimal value 0x5A without any casting but for 0x9C the compiler emit errors saying it is an integer?

Am i missing something?

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your missing that Byte are signed : (8 bits, all 1 is -1 !)

So byte range is -128 -> 127
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In other words, the maximum value you can store in a byte is 127, NOT 255.

Layne
 
Richard West
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

In that case let's say if i have the number -8 and +10.
How do i convert them to signed bytes respectively?

Richard West
[ October 14, 2005: Message edited by: West Richard ]
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Richard,

Please check your private messages.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by West Richard:
Hi everyone,

In that case let's say if i have the number -8 and +10.
How do i convert them to signed bytes respectively?

Richard West

[ October 14, 2005: Message edited by: West Richard ]



Even better than the above, you can just do this:

This works because these numbers are in the range of valid values for bytes. Also, they are already *signed*.

Perhaps you meant "unsigned" instead. If so, assigning these values to variables is the same, assuming you are actually interested in the bit pattern of the value -8, for instance. Once you do the assignment, you just treat the variable as if its unsigned. You can do any bit-wise operations you wish, as long as you cast the result back to a byte. (The result of &, |, ^, >>, and << is promoted to an int for some reason.)

I hope this answers your question. If not, perhaps you can illustrate what you are trying to do with some more specific code.

Layne
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic