General Java Question concerning byte operator and byte rotation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Winterrage
    New Member
    • Sep 2007
    • 8

    #1

    General Java Question concerning byte operator and byte rotation

    Greetins everyone,

    I'm trying to understand the logic in some operation with different byte operator.

    I'm looking at:
    http://www.exampledepo t.com/egs/Programs/sudoku_solver_S udokuSolver.htm l

    More specifically, this operation from the set function:

    boolean canSet = cells[loc] == 0
    && (colsSet[c] & (1<<num)) == 0
    && (rowsSet[r] & (1<<num)) == 0
    && (subgridSet[blockLoc] & (1<<num)) == 0;
    if (!canSet)
    {
    return false;
    }

    From this function: public boolean set(int loc, int num)
    colSet and rowSet are filled with a lots of 0.

    The question is what is colsSet[c] & (1<<num) used for?

    Thanks again :)
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Winterrage
    Greetins everyone,

    I'm trying to understand the logic in some operation with different byte operator.

    I'm looking at:
    http://www.exampledepo t.com/egs/Programs/sudoku_solver_S udokuSolver.htm l

    More specifically, this operation from the set function:

    boolean canSet = cells[loc] == 0
    && (colsSet[c] & (1<<num)) == 0
    && (rowsSet[r] & (1<<num)) == 0
    && (subgridSet[blockLoc] & (1<<num)) == 0;
    if (!canSet)
    {
    return false;
    }

    From this function: public boolean set(int loc, int num)
    colSet and rowSet are filled with a lots of 0.

    The question is what is colsSet[c] & (1<<num) used for?

    Thanks again :)
    It's a bit of 'bit flag packing'. The expression '1<<num' shifts the number/bit 1
    to the left by 'num' positions; here's a little table:

    Code:
    num= 0: result: 0x01
    num= 1: result: 0x02
    num= 2: result: 0x04
    num= 3: result: 0x08
    num= 4: result: 0x10
    num= 5: result: 0x20
    num= 6: result: 0x40
    num= 7: result: 0x80
    I bet colSet and rowSet are int or byte arrays where each bit of every element
    of the array is used as a 'boolean' flag.

    The '& operator checks a single bit here: it is either zero or not.

    The code fragment must have something to do with a chess board or similar.

    kind regards,

    Jos

    Comment

    • Winterrage
      New Member
      • Sep 2007
      • 8

      #3
      Thanks a lot Jos :)

      It's a Sudoku table, I understand what is going on with that part.

      And now this part is bugging me:

      colsSet[c] ^= (1<<num);
      rowsSet[r] ^= (1<<num);
      subgridSet[blockLoc] ^= (1<<num);

      Any other help would be appreciated!

      Thanks again

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Winterrage
        Thanks a lot Jos :)

        It's a Sudoku table, I understand what is going on with that part.

        And now this part is bugging me:

        colsSet[c] ^= (1<<num);
        rowsSet[r] ^= (1<<num);
        subgridSet[blockLoc] ^= (1<<num);

        Any other help would be appreciated!

        Thanks again
        Those expressions 'toggle' the value of a particular bit: if it was one (1) it becomes
        equal to zero (0) and vice versa. Have a look the the Java artices series. There's
        an article in there describing a Sudoku solver; it uses plain booleans instead of
        those bitflags.

        kind regards,

        Jos

        Comment

        • Winterrage
          New Member
          • Sep 2007
          • 8

          #5
          Originally posted by JosAH
          Those expressions 'toggle' the value of a particular bit: if it was one (1) it becomes
          equal to zero (0) and vice versa. Have a look the the Java artices series. There's
          an article in there describing a Sudoku solver; it uses plain booleans instead of
          those bitflags.

          kind regards,

          Jos
          Thanks a lot, i'll take a look at that :)

          Comment

          Working...