• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why use the use of >>>?  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys I really would like to know the use of >>> in the write() methods in I/O when using the DataInputStream class,

why can't the data just be converted to normal binary without shifting the bits multiple times and "anding" OxFF  to it?

I got an answer on Udemy but it didn't really help me out because I wouldn't be able to write a custom method to print data to a dat file

the answer I got was this and he is right but I'm not sure how it would even work




Hi,

just try it and see what happens when you read the file. I think that will give you really clear explanation. It is really good to try things out that way you will learn a lot.

Hope that helps.





thanks
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the code you are talking about?
Also copy the program's output and paste it here so we can see it.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


it creates a file with non readable input,





in the writeInt() method of the DataOutputStream class
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
** edit I meant data OutputStream class not Input @ the original post I made
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What class is the variable: out?

a file with non readable input,

What does that mean? 
Are you saying the file does not contain text?

data just be converted to normal binary

What is "normal binary"?
Everything in a computer is in binary.  It is only when you want to see it as a String that the binary is converted to characters.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the out is from the DataOutputStream class

it does contain text but it's a dat file so a lot of it will be non readable for humans anyway

I mean by normal binary,I should say it's normal binary value,why does the bits need to be shifted a number of times and "&" with 0xFF

thanks
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why does the bits need to be shifted a number of times and "&" with 0xFF 

An int contains 4 bytes of data.  The write method writes a byte to output.  To get all 4 bytes from the int, each byte in the int needs to be shifted so its 8 bits are in the lower 8 bits of the int that is to be written. The AND of 0XFF strips off all bits but what is in the lower 8 bits.


Try writing a simple program that starts with an int set to 0x12345678, use the shift and AND statements and print the results to see what happens.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Norm that makes sense but why wouldn't the write method just write an int to output 4 bytes instead of a byte?

 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To write an int, the code breaks the int down into 4 bytes and writes them one by one.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a program to output a number 0x12345678 then <<< 1 on it and anded with 0xFF

I got the result 60?

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The printed value was in decimal.
Time to look at some bits.  Use the Integer class's toBinaryString() to print the value of i before and after the change
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the output

10010001101000101011001111000 // before
111100 // after

I also noticed it's 6 bits instead of 8?
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing it was 6 bits in the output because it shaved the leading 0's off

but yeah it shifted each number right
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it shaved the leading 0's off

Yes, the print method does that
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!