Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Practical use of operators...

Tina Long
Ranch Hand
Posts: 36
I'm reading the java tutorial on Sun's site - and Im trying to apply what I'm learning...but I'm having a disconnect on a few things that I'd like a little explanation on...if possible.

When would you use the modulus (%) operator? Like what is a good real-world example as to when you would want to use this?

Also - do people really use the bitwise manipulations? Is it a common practice? The example as to why a person would use it is if you have a bunch of boolean flags in your code - you can set a bunch of constantants and then compare them to a variable whose bits would be set according to the state of each flag. This just seems so confusing - and I'm just wondering how often is this really used in the "real world"?

Ko Ko Naing
Ranch Hand
Posts: 3178
Originally posted by Tina Long:
When would you use the modulus (%) operator? Like what is a good real-world example as to when you would want to use this?

When you want to find even and odd numbers, you'll use theNumber%2==0 for even number and else for odd numbers. They are pretty useful, when you want to color your table in alternate row color.

Steven Bell
Ranch Hand
Posts: 1071
When you're talking computer programming "real world" can mean alot of things. I have personally used both % and ~, >>, <<, >>> operators. Most of the bitwise operators were when modeling game boards, or other game related apps. The % operator I see used all the time.

M Beck
Ranch Hand
Posts: 323
when you've got an integer number of seconds, and want to represent it as hours:minutes:seconds, you can use integer division and modulus. similarly for some measuring systems, for example figuring yards/feet/inches out of inches alone. (in the metric system, of course, this is much less of a concern.)

really, the modulus operator is surprisingly useful; i use it almost as much as exponentiation, more than exclusive-or, and certainly much more than any of the bitwise-logic operators. it might help to spend a few hours studying the mathemathics of modulus arithmetic, however.

Tina Long
Ranch Hand
Posts: 36
When I said "real world" I mean - outside of a classroom - and at a business of some sort. I know that "real world" is a very generic term...

M Beck
Ranch Hand
Posts: 323
the "real world" is a somewhat slippery term, but i do believe parts of it still includes distance and time calculations. games programming, too. (i suppose you could use bit fields in implementing your own data types anywhere you want to keep track of a large number of boolean variables, but i'd recommend against it -- too confusing.)

it certainly includes interfacing with non-Java libraries using JNI. and if the libraries you want to access are written in C, then they might use flags implemented as bit fields, in which case you'll use bitwise logic in the interfacing code at least. (please don't let that propagate into Java code, though!)

more generally, modulus calculations are useful any time you want to break up a large quantity into several smaller "units", and wish to know how far filled up the last, fractional "unit" will be. that goes not only for distance and time, but read/write buffers, characters/lines of text, and countless other things.

bitwise shifting used to be a common trick in low-level code for division and multiplication by powers of two, but nobody does that by hand any longer; it's become a common optimization trick every good compiler and virtual machine should use automatically without being told. then again, compilers and virtual machines still have to be written by somebody, and they're surely real world programs...

Joel McNary
Bartender
Posts: 1840
I've never seen bitwise shifting used, but bitwise & and | operators can be useful. If you have a scheduling program and need to record the days of the week that something takes place, you have a few options:

1). Store separate boolean fields for Monday, Tuesday, Wednesday, etc.

2). Store a String field that looks like "MT-W--SS". Use charat(index) to see if a given day is selected.

3). Map each day as a power of two:

MONDAY = 1, TUESDAY = 2, WEDNESDAY = 4, etc.
Use bitwise & and | to set the field's value: days = MONDAY|TUESDAY|WEDNESDAY|SATURDAY|SUNDAY (01100101, or 101)

To check if a day is valid, say: boolean isValid = (value&THURSDAY) > 0;

This latter technique is quite powerful, as you can add schedules together.

int schedule1 = MONDAY|TUESDAY|WEDNESDAY|SATURDAY|SUNDAY (101)
int schedule2 = SUNDAY|TUESDAY|THURSDAY (01001010, or 74)

int combined schedules = schedule1 | schedule2 (011011011, or 109)

Tina Long
Ranch Hand
Posts: 36
Thank you all for your replies. I talked with my boss and he told me that bitwise shifting is not something we utilize in our company and it's not really used much elsewhere - but I need to know it for the exam - when I take it.

He then gave me an "assignment" to create a deck of cards and shuffle and deal them - and use the % operator to assign a suit and face to the cards. I will be working on that here shortly. So I can see the use of the operator. He said that it's a very useful operator.

Thanks again for all your replies.