x404.co.uk
http://www.x404.co.uk/forum/

Bitwise conversions
http://www.x404.co.uk/forum/viewtopic.php?f=4&t=16202
Page 1 of 1

Author:  Fogmeister [ Tue Apr 03, 2012 10:34 pm ]
Post subject:  Bitwise conversions

If I have a bit mask (i.e. a decimal conversion of a binary number) and I want to find the ...

Ah, I think I've got it :D Thanks!

:lol:

Author:  Fogmeister [ Wed Apr 04, 2012 1:58 pm ]
Post subject:  Re: Bitwise conversions

OK, got a question now :D

It is bitwise operator related but be thinking about sudoku to put it in context.

I will be comparing three cells.

Cell a will have the definite number 7 in it (for example).
Cell b will have the definite number 5 in it.
Cell c will have the candidate numbers 1, 3, 5 and 7 in it.
They can all "see" each other (i.e. in the same box, row or column).

At the moment my loop runs and I end up so that...

Cell a = 7
Cell b = 5
Cell c = 1,3 (i.e. it has had 5 and 7 eliminated)

BTW (7 is actually 64 and 1,3 is actually 5 (bitwise)).

That's all very good and VERY quick but at the end I want to see what has been removed.

If I take a copy at the beginning so I have ...

origCell a = 7
origCell b = 5
origCell c = 1,3,5,7

How can I compare cell c and origCell c to see what has been eliminated?

cell c = 1,3 = 5 = 000000101
origCell c = 1,3,5,7 = 85 = 001010101

Ah, XOR! cell c XOR origCell c = 001010000 = 80

Is that right?

Author:  Fogmeister [ Wed Apr 04, 2012 2:15 pm ]
Post subject:  Re: Bitwise conversions

Ah, I can then get the individual bits using a shift operator.
Code:
int dif = the xor that gives me the numbers that have been removed.

    for(int i = 1; i <= 9; i++)
    {
        int bit = dif & 1;

        if (bit == 1)
            cout << "elim " << i << endl;

        dif = dif >> 1;
    }


This lists all the numbers (bits) that were eliminated from the given cell.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/