Help coderanch get a
new server
by contributing to the fundraiser
Plus win three weeks of access to Security in Spring Boot REST Web Service Applications this week in the Spring forum!
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

# Shift operator conversions

Ranch Hand
Posts: 167
• Number of slices to send:
Optional 'thank-you' note:
I feel lost when i have to convert a number into a byte representaion from an int.Like if int i=168 then how to convert it into -88 in 8 bits?

Another issue is if a byte =<some number exceeding 127> how to know what will be its equivalent form in byte representaion,
say eg. System.out.println((byte)128);
and System.out.println((byte)-128);

Is there any quick tip for such conversions please....

Greenhorn
Posts: 6
• Number of slices to send:
Optional 'thank-you' note:
168 has the 10101000 representation in binary. I think that the first byte(1) means it is a negative number. Then the bits of the number left (0101000) will be inversed. 0101000 -> 1010111. 1010111 is 88 in binary. The same happens with 128. 128 is represented 10000000 in binary. Fallowing this rule from 10000000 (- minus because the first digit is one)and inversing the rest of the number we obtain 1111111(127 in decimal). I don't know if this rule remains for large numbers.

ioanis siafu
Greenhorn
Posts: 6
• Number of slices to send:
Optional 'thank-you' note:
I was wrong in my explications. The modulus of the two numbers added is 256.

Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:
168 is represented as 10101000 in binary.The most significant bit is the sign bit which means that 10101000 is actually a negative number and not 168.To get the negative number, first take the one's complement of 10101000,
ie 01010111. now add 1 to this which gives 01011000 which is 88.
So the number represented by 10101000 is actually -88.

Nikhil Raj
Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:
for larger numbers only their least significant 8 bits are taken..
the rest are discarded.

Sheriff
Posts: 13411
• Number of slices to send:
Optional 'thank-you' note:
"Nikhil Raj R S",
Please check your private messages regarding an important administrative matter.
-Ben

 Anything worth doing well is worth doing poorly first. Just look at this tiny ad: We need your help - Coderanch server fundraiser https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
Similar Threads