for (int i = 1; i <= 0x20000; i = i << 1)

jpassenger
2006-08-21
2013-04-25
  • jpassenger

    jpassenger - 2006-08-21

    Looking in JSpellChecker.java I'm wondering what the heck this actually does?

    I've been coding Java for years but its too obscure for me.

      public void setOptions(int options) {
        //System.out.println("in " + getClass().getName() + ".setOptions(" + Integer.toBinaryString(options) + ")");
        Configuration config = checker.getConfiguration();
        for (int i = 1; i <= 0x20000; i = i << 1) {
          String key = (String) configmap.get(new Integer(i));
          if (key != null) config.setBoolean(key, (options & i) > 0);
        }
      }

    No activity for ages, is Jazzy dead?

    Josh

     
    • Raymond Martin

      Raymond Martin - 2006-11-19

      int i = 1; i <= 0x20000; i = i << 1

      i is incremented by doubling it each time through the loop.
      Doubling is achieved by left bitshifting by 1 place each time.
      The loop exits once i is greater than 20000 hex = 0010 0000 0000 0000 000 bin
      So the maximum number of shifts is 16 and the loop executes 17 times.

      The code is setting truth values for particular keys, when they exist.
      (options & i) is an AND masking, everything except the one bit in i
      and "options" if it is true will be set to 0. If the result is not 0, then
      the truth value will be set with the (options & i) > 0 argument, else it
      be false.

      Raymond

       

Log in to post a comment.