• 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

Q about bitwise operators

 
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello folks!
I have two questions concerning bitwise operators, and my reason for posting it here is that I'll soon start to prepare for the SCJP.
1. What do you use bitwise operators for?
I know that "they allow you to manipulate individual bits in an integral primitiv data type." and if I should translate that, then I'd say that they allow you to turn specific bits on/off within an integer..
is that true? and if so.. why would you want to do that. This might seem as a stupid question, but im a wee bit confused about their "function"
2. Are bit wise operators importent in the programmer certification ?
Thanks in advance
/Svend
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me start by saying that bitwise operators will not "make or break" your chances of passing the certification exam, but count on having a question or two pertaining to them. Frankly, I think they're pretty easy to learn so setting aside a little time to learn them will give you a few free points on the exam.
Now, for the other part of your question, what might you use a bit operation for... Well, there are really a number of uses for bit operations. Most often, however, bits are only addressed when you get to the lowest levels of computation and, Java, being a very high level language, seldom deals with individual bits. However, there are still some uses for a bit operation in Java programs.
One example - quick doubling. What happens when you shift a number to the left one position? You double the number. Look at this example:

Any time you want to multiply a number by two, you can use a simple bit shift in order to perform the multiplication very quickly. I'm not sure (someone please let me know if you know for sure), but I'm skeptical that a good Java compiler might replace a multiplication by 2 with a bit shift, but I'm not positive. Regardless, this is a very efficient way to do a fairly common task.
Another use of bit operations is in the use of bit masks. Let's say that you have a very large number of related values that you need to keep track of. As an example, let's say we're tracking the options on a new car. There could be tens or hundreds of options, from a rear spoiler to an AM/FM stereo. Rather than having hundreds of individual booleans, we could use a bit mask to save space.
The concept behind a bit mask is to use a larger unit of data, such as a byte, and designate each bit to represent something in particular. For a simple example, let's say that we want to track whether or not a vehicle has an AM/FM stereo, a CD player, cruise control, and air conditioning. We'll use a single byte to store all of these values (a byte contains 8 bits so we're really only going to use half of it). We could do so like this:

This code (although it's untested), will print out that auto 1 has an AM/FM stereo, while auto 2 does not. We're using just 1 bit of a larger data type to store that information. Although this approach may be a little more confusing than using 4 booleans, it can be a big help when it comes to systems that are short of memory or storage. All in all, using bit-masks can be a very graceful solution to many problems.
Hopefully, that helps. Even if you don't have the need to use bit operations often (frankly, I don't use them that often), they're very useful to know and you'll definitely see a few questions about them on the exam.
I hope that helps,
Corey
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot Corey for the great answer..
I especialy liked the examples.
/Svend
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic