Results 1 to 7 of 7

Thread: Boolean Logic

  1. #1
    DaveC is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default Boolean Logic

    So why is it that in C++ you can have the code:

    Java Code:
    int x = 0x40000000;
    int y = 0x20000000;
    int z = x|y;
    
    if (z & y) 
         Print("Z masks Y");
    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.

    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.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    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++.

  4. #4
    DaveC is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Or use enums.
    Sorry, enums?
    Quote Originally Posted by Junky View Post
    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++.
    I'm not moaning. I didn't know; that's why I asked, and find it rather rude that instead of trying to help me you troll the thread.
    Last edited by DaveC; 06-15-2011 at 05:07 AM.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Sorry, enums?
    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).

    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));
        }
    }

  6. #6
    DaveC is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    Ah, thank you much, pbrockway2. This will do just fine.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Similar Threads

  1. boolean error help when no boolean is given
    By drewtrcy in forum New To Java
    Replies: 18
    Last Post: 05-05-2011, 09:04 AM
  2. Need help in logic
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 06:44 PM
  3. Need help on logic
    By nn12 in forum New To Java
    Replies: 6
    Last Post: 03-10-2011, 11:06 AM
  4. need a logic for this
    By rajivjoshi in forum New To Java
    Replies: 4
    Last Post: 06-12-2010, 02:18 PM
  5. Cant get the logic right
    By jermaindefoe in forum New To Java
    Replies: 4
    Last Post: 03-11-2008, 12:22 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •