Results 1 to 7 of 7
Thread: Boolean Logic
- 06-15-2011, 04:09 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
Boolean Logic
So why is it that in C++ you can have the code:
But a similar check ( the "z & y" part ) in java forces you to have booleans ("found int but expected boolean") to do the algebra? I know they're boolean operators. Is there another way to do comparisons like that? In the program I'm writing I was planning to use certain integers (like x and y here) to flag for certain properties, but found Java won't let you.Java Code:int x = 0x40000000; int y = 0x20000000; int z = x|y; if (z & y) Print("Z masks Y");
Additionally, is it possible to overload operators if I can't get the comparisons to work this way (i.e. the functionality isn't already there in another form)? I'd like to overload the "&" operator to do this if it can't; I know the method to do so.Last edited by DaveC; 06-15-2011 at 04:18 AM.
- 06-15-2011, 04:37 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
C's if allows an int expression: it is tested for ==0 ("false") or !=0 ("true"). Java doesn't do this, perhaps to avoid mistakenly writing if(a=b) when you mean if(a==b) but in any event "if" is treated as something that acts on a boolean and boolean expression you must give it.
Consider (a&b)==0, (a|b)!=0, (a&b)==a etc. Or use enums or BitSet.
[Edit] You can't overload operators.Last edited by pbrockway2; 06-15-2011 at 04:40 AM.
- 06-15-2011, 04:43 AM #3
It's amazing how many people moan about how Java doesn't do what C/C++ does. Well if it could then it wouldn't be Java it would be C/C++.
- 06-15-2011, 04:43 AM #4
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
- 06-15-2011, 05:02 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Instead of taking a bunch of things you might want to signal and assigning them int numbers (1, 2, 4, 8, ...) then shifting and anding etc, you can create an enum and then use an instance of EnumSet (like a bitset but strongly typed).Sorry, enums?
Java Code:import java.util.EnumSet; public enum TeaAdditives { WATER, MILK, LEMON, SUGAR; public static void main(String[] args) { EnumSet<TeaAdditives> order = EnumSet.of(MILK, SUGAR); System.out.println("Has sugar: " + order.contains(SUGAR)); System.out.println("Has lemon: " + order.contains(LEMON)); } }
- 06-15-2011, 05:06 AM #6
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
Ah, thank you much, pbrockway2. This will do just fine.
- 06-15-2011, 05:11 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Similar Threads
-
boolean error help when no boolean is given
By drewtrcy in forum New To JavaReplies: 18Last Post: 05-05-2011, 09:04 AM -
Need help in logic
By nn12 in forum New To JavaReplies: 3Last Post: 03-23-2011, 06:44 PM -
Need help on logic
By nn12 in forum New To JavaReplies: 6Last Post: 03-10-2011, 11:06 AM -
need a logic for this
By rajivjoshi in forum New To JavaReplies: 4Last Post: 06-12-2010, 02:18 PM -
Cant get the logic right
By jermaindefoe in forum New To JavaReplies: 4Last Post: 03-11-2008, 12:22 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks