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.
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:
Joined: Jan 21, 2003
I greatly appreciate all of your help. You guys rock!
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 firstname.lastname@example.org http://www.patrickkellogg.com
Patrick Kellogg<br />3186 West 36th Avenue<br />Denver, CO 80211<br />303-480-0890<br />email@example.com<br />http://www.patrickkellogg.com
Joined: Nov 09, 2000
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 ]