70

What?!?

There is an ^= operator in Java for booleans. I have been programming in Java for 4 years now and never knew this. Like b ^= true will flip b.

Mind blown; this is the greatest thing ever

Comments
  • 23
    &= and-assignment
    |= or-assignment
    ^ = xor-assignment
    <<= left-shift-assignment
    >>= right-shift-assignment
  • 2
    My mind is blown. I love you.
  • 3
    But 'b != b' works fine too and looks clearer?
  • 17
    @Gatgeagent I believe you meant 'b = !b'
  • 4
    Yep why not just b =!b?
  • 4
    @Salmakis because some people just want to show off their bitwise twiddling skill
  • 2
    Logical XOR is just the same as not equal.

    true != true : false
    true != false : true
    false != true : true
    false != false : false

    So for booleans, a ^ b is equivalent to a != b. Though there's no compound assign for not equal, so it is very slightly more convenient in some cases.
  • 0
    @bkwilliams bit operators... i've never used them.... and i dont know how or why i need them... am i a bad dev?
  • 2
    @billgates it's a hold over from C but now they are used to make jr devs ask questions during code review.
  • 2
    @bkwilliams @billgates Well they are Definitely still used and useful for many more advanced things. If you want to implement a binary format of any kind they are everything short of necessary. A good, simple example would be implementing compression like lz4.
  • 0
    @billgates @bkwilliams @jchw They are incredibly useful for competitive programing as well. Also good in optimizing some stuff, making them take logarithmic time (via comparing binary representation of the start and end and doing computations on those) vs linear (via going through all numbers).
  • 0
    @BingBangTheory cool, however my scope is usually higher so they are not leveraged.
  • 0
    Bro, are you kidding me? That's one of the first things I read in my freshman year syllabus.
  • 0
    im pretty sure the guys that said "i never used bitwise" had used thrm without even knowing about it. example with flag settings with |
Add Comment