File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Byte-How to use bitwise operators? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Byte-How to use bitwise operators?" Watch "Byte-How to use bitwise operators?" New topic
Author

Byte-How to use bitwise operators?

Sloan Bowman
Ranch Hand

Joined: Jan 21, 2003
Posts: 107
I consider my self to know quite a bit about java and I develop J2EE applications daily but never use the byte type. In what circumstances would you use the byte type and or bitwise operators? Any examples would be greatly appreciated.
Pin Ting
Greenhorn

Joined: May 28, 2003
Posts: 22
I use bitwise operation in masking and byte in network programming.


Never do things the hard way if you don't have a good reason.
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
A typical use of the byte primitive is to read from and write to streams. See InputStream and OutputStream in the Java API docs.
As far as the use of bitwise operators go, there are lmiitless situations in which these might be useful. If you look at the fifth post from the bottom in this thread from the "Programming Diversions" forum, you will see the bitwise operators used quite a bit in the CharacterGrid2 class.
In that class I am using a single int (32 bits) to store a grid reference made up of two 16 bit numbers. That is, I'm packing two 16 bit values into one 32 bit value. In order to manipulate the individual components of this packed grid reference, bitwise operations are required.
Here's the bit of code from that class which takes a row and a column coordinate (positive or negative) no larger than 15 bits in size and packs them into a single 32 bit int.

Retrieving the individual row and column components from a packed reference:

HTH,
Jason
Sloan Bowman
Ranch Hand

Joined: Jan 21, 2003
Posts: 107
I greatly appreciate all of your help. You guys rock!
Patrick Kellogg
Greenhorn

Joined: Mar 01, 2002
Posts: 1
I think this will help me, too.
I've been trying to read (and write) large-scale images quickly from a disk. They are stored as an array of integers. My first attempt was to use FileReader and FileWriter. However, reading everything character-by-chracter was too slow. So, I've been trying to use a ByteArrayInputStream.
My problem is that the file now comes in as a byte array, but the image I'm trying to modify is an array of ints. In C or C++, I would cast a pointer to bytes into a pointer to int and get the conversion for "free".
I've been trying to find a way to mask a byte array over an int array, but Java's not able to do a cast like that, it seems. Any ideas? Should I shift the byte array over by 4 values every time, and suck out the ints one by one into a new array? It's taking a long time that way, but it's still faster than FileReader's I/O.
Patrick Kellogg
kellogg@dimensional.com
http://www.patrickkellogg.com


Patrick Kellogg<br />3186 West 36th Avenue<br />Denver, CO 80211<br />303-480-0890<br />kellogg@dimensional.com<br />http://www.patrickkellogg.com
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Originally posted by Patrick Kellogg:
My problem is that the file now comes in as a byte array, but the image I'm trying to modify is an array of ints. ...

How about writing your own IntegerArrayInputStream extending InputStream? If you take a look at the source code for ByteArrayInputStream, which really doesn't seem all too involved, you should be able to accomplish what you want with little difficulty.
[ September 26, 2003: Message edited by: Jason Menard ]
 
Consider Paul's rocket mass heater.
 
subject: Byte-How to use bitwise operators?