• 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

Byte-How to use bitwise operators?

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use bitwise operation in masking and byte in network programming.
 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I greatly appreciate all of your help. You guys rock!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic