Help coderanch get a
new server
by contributing to the fundraiser
  • 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

demonstrate how to convert step by step 130 to fit in a byte variable

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
someone, please, can demonstrate how the cast is made of the number 130 to fit into a variable of type byte? Step by step ....

I try begin doing this:

130 / 2 = 65
Mod 0
65 / 2 = 32
Mod 1
32 / 2 = 16
Mod 0
16 / 2 = 8
Mod 0
8 / 2 = 4
Mod 0
4 / 2 = 2
Mod 0
2 / 2 = 1
Mod 0
1 / 2 = 0
Mod 1

then 130 = 1000 0010

then....???
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

I'm not sure exactly what you want to know. The value 130 does not fit into a byte, because a byte is an 8-bit signed data type - it can contain only values between -128 and 127. Values in a byte (as well as in int, short and long) are stored in two's complement format.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is possible to use a byte to store that bit pattern, but Java will interpret it as a signed two's complement integer, and you'll end up with a negative value: -126.
However, if you know the byte's bit pattern should be interpreted as an unsigned value, you can work around this.
All you need is to widen from a byte to an int and do some bit twiddeling:



I guess the real question is, why would you want to do this?
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you wrote 130 in binary representation is

1 0 0 0 0 0 1 0

the leftmost bit is set to 1, so the number will be negative

now flip all the bits

0 1 1 1 1 1 0 1

add 1

0 1 1 1 1 1 1 0

in decimal is 126 but remember that the leftmost bit was 1 then the number is negative

= - 126
 
pedro abs
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nicola Garofalo wrote:As you wrote 130 in binary representation is

1 0 0 0 0 0 1 0

the leftmost bit is set to 1, so the number will be negative

now flip all the bits

0 1 1 1 1 1 0 1

add 1

0 1 1 1 1 1 1 0

in decimal is 126 but remember that the leftmost bit was 1 then the number is negative

= - 126





That's what I want to understand.

(why casting 130 to byte results -126)

But why I have to flip all the bits and then add 1?
how this algorithm?

Thanks everybody !!!
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the two's complement system.

Follow the link that Jesper Young posted in one of the previous messages

I think there you will find all you need to know about it.



http://en.wikipedia.org/wiki/Two's_complement
 
Look! It's Leonardo da Vinci! And he brought a tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic