I'm trying to understand the bitwise and the shift operators. I wrote a simple code to show me the bits in a short type.
class Shift {
public static void main (String args[]) {
short b = 16384;
for (int t = 32768; t > 0; t = t / 2) {
if ((b&t) != 0) System.out.print("1 ");
else System.out.print ("0 ");
}
System.out.println();
b = (short)(b + 2);
for (long t = 2147483648L; t > 0; t = t / 2) {
if ((b&t) != 0) System.out.print ("1 ");
else System.out.print ("0 ");
}
System.out.println();
}
}
And the output is:
C:\>java Shift
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0
I used with the second for an AND with a short (16 bits) and a long (64 bits) and the output is 32-bits.
I don't understand why the output of the second for is 32-bits.
Thank you.
You start your loop with long t = 2147483648L, which is 2^31. Therefore your loop has 32 iterations and prints 32 bits.
If you wish to display more bits, start the loop with long t = 0x4000000000000000L; (which is equivalent to the binary number starting with 01 and ending with 62 0s).
Related
I am trying for hours to print DFS trace (including when you get stuck and you have to backtrack) but my output is always missing a 0 and having double numbers
My input matrix is like this
0 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0
0 1 0 0 1 0 1 0
1 0 1 0 0 1 0 0
0 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0
0 0 1 0 0 1 0 0
0 0 0 0 0 1 1 0
My output is like this
0 0 1 0 2 2 4 4 5 2 6 3 7
But it should be like this
0 1 0 2 4 5 4 2 6 2 0 3 7
My algoritem is this one
public void DFSDriver(int[][] adjMatrix)
{
int[] visitMatrix = new int[adjMatrix[0].length];
DftRecursive(adjMatrix, visitMatrix, 0); //Visit all nodes you can
//Visit the ones you cannot because they are separate
for(int i = 0; i < adjMatrix.length; i++) {
if(visitMatrix[i] != 1) {
DftRecursive(adjMatrix, visitMatrix, i); //Visit the ones you cannot because they are separate
}
}
}
public void DftRecursive(int[][] srcMatrix, int[] visitMatrix, int vertex)
{
visitMatrix[vertex] = 1;
System.out.print(vertex + " ");
for (int neighbour = 0; neighbour < srcMatrix[0].length; neighbour++)
{
if (srcMatrix[vertex][neighbour] == 1 && visitMatrix[neighbour] == 0)
{
System.out.print(vertex + " "); //If I don't print this DFS by itself works fine, but I want to print this because I want to backtrack when I get stuck
DftRecursive(srcMatrix, visitMatrix, neighbour);
}
}
}
Hope someone can figure out what I am doing wrong here (the problem is that I also need to print the backtracking, printing only DFS is fine, but backtracking is harder to do)
exchanging the print line and the DftRecursive line.
like this:
if (srcMatrix[vertex][neighbour] == 1 && visitMatrix[neighbour] == 0)
{
DftRecursive(srcMatrix, visitMatrix, neighbour);
System.out.print(vertex + " ");
}
I am currently trying to make my chess engine faster, and am looking at implementing magic bitboards for my sliding piece attack generation. I am using a bitboard representation of the chessboard with the a1 square being the furthest right bit, and h8 being the furthest left. I am looking at this site:
https://rhysre.net/fast-chess-move-generation-with-magic-bitboards.html#:~:text=A%20bitboard%20is%20simply%20a,and%20bit%2063%20%3D%20h8)
Specifically the code snippet found towards the bottom of the page that reads:
U64 getBishopAttacksMagic(int square, U64 blockers) {
// Mask blockers to only include bits on diagonals
blockers &= BISHOP_MASKS[square];
// Generate the key using a multiplication and right shift
U64 key = (blockers * BISHOP_MAGICS[square]) >> (64 - BISHOP_INDEX_BITS[square]);
// Return the preinitialized attack set bitboard from the table
return BISHOP_TABLE[square][key];
}
I already have Shallow blues magic numbers(each number corresponding to a square), and I already have pre initialized attack masks for the bishop piece stored in a 64 length array(again each number corresponding to a square). So i know how to get the key. But how do I generate the last array which takes the key, "BISHOP_TABLE" array? I do not understand how to generate that 2d array given an attack mask and magic number for each square. Thank you for your help in advance.
For each square, you need to generate every permutation of blocking pieces inside the bishop mask. For example, using this mask for the square e4 (#28):
8 | 0 0 0 0 0 0 0 0
7 | 0 1 0 0 0 0 0 0
6 | 0 0 1 0 0 0 1 0
5 | 0 0 0 1 0 1 0 0
4 | 0 0 0 0 0 0 0 0
3 | 0 0 0 1 0 1 0 0
2 | 0 0 1 0 0 0 1 0
1 | 0 0 0 0 0 0 0 0
---------------
a b c d e f g h
since there are 9 set bits, there are 2^9 = 512 different patterns of blocking pieces. The permutation number 339 (as binary = 0b101010011) looks like this:
8 | 0 0 0 0 0 0 0 0
7 | 0 1 0 0 0 0 0 0
6 | 0 0 1 0 0 0 0 0
5 | 0 0 0 1 0 0 0 0
4 | 0 0 0 0 0 0 0 0
3 | 0 0 0 0 0 0 0 0
2 | 0 0 1 0 0 0 1 0
1 | 0 0 0 0 0 0 0 0
---------------
a b c d e f g h
Bits are read from right (lsb) to left (msb) in the number and are set in the mask from the a file to the h file, 1st rank to 8th. Permutation 0 is an empty board and 511 (0b111111111) is the full mask.
Here's a method that takes a permutation number along with the bishop mask and returns the corresponding blockers bitboard:
private static long blockersPermutation(int iteration, long mask) {
long blockers = 0;
while (iteration != 0) {
if ((iteration & 1) != 0) {
int shift = Long.numberOfTrailingZeros(mask);
blockers |= (1L << shift);
}
iteration >>>= 1;
mask &= (mask - 1); // used in Kernighan's bit count algorithm
// it pops out the least significant bit in the number
}
return blockers;
}
Using this we can calculate the keys with both the magic numbers and the blockers, and we can create their corresponding values, the attack masks.
For each permutation of blocking pieces, create the corresponding attack mask and store it in the table. Include the blocking pieces and the squares on the side of the board in the mask. The attack mask for the blockers #339 on square #28 is:
8 | 0 0 0 0 0 0 0 0
7 | 0 0 0 0 0 0 0 1
6 | 0 0 0 0 0 0 1 0
5 | 0 0 0 1 0 1 0 0
4 | 0 0 0 0 0 0 0 0
3 | 0 0 0 1 0 1 0 0
2 | 0 0 1 0 0 0 1 0
1 | 0 0 0 0 0 0 0 0
---------------
a b c d e f g h
Here's a Java method to initialize the 64 bishop lookup tables:
private final long[][] BISHOP_LOOKUP = new long[64][512];
private static int getFile(int square) {
return square % 8;
}
private static int getRank(int square) {
return square / 8;
}
private static int getSquare(int rank, int file) {
return rank * 8 + file;
}
// just like the code snippet, generates the key
private static int transform (long blockers, long magic, int shift) {
return (int) ((blockers * magic) >>> (64 - shift));
}
private void initBishopLookup() {
for (int square = 0; square < 64; square++) {
long mask = BISHOP_MASKS[square];
int permutationCount = (1 << Long.bitCount(mask));
for (int i = 0; i < permutationCount; i++) {
long blockers = blockersPermutation(i, mask);
long attacks = 0L;
int rank = getRank(square), r;
int file = getFile(square), f;
for (r = rank + 1, f = file + 1; r <= 7 && f <= 7; r++, f++) {
attacks |= (1L << getSquare(r, f));
if ((blockers & (1L << getSquare(r, f))) != 0) {
break;
}
}
for (r = rank - 1, f = file + 1; r >= 0 && f <= 7; r--, f++) {
attacks |= (1L << getSquare(r, f));
if ((blockers & (1L << getSquare(r, f))) != 0) {
break;
}
}
for (r = rank - 1, f = file - 1; r >= 0 && f >= 0; r--, f--) {
attacks |= (1L << getSquare(r, f));
if ((blockers & (1L << getSquare(r, f))) != 0) {
break;
}
}
for (r = rank + 1, f = file - 1; r <= 7 && f >= 0; r++, f--) {
attacks |= (1L << getSquare(r, f));
if ((blockers & (1L << getSquare(r, f))) != 0) {
break;
}
}
int key = transform(blockers, BISHOP_MAGICS[square], Long.bitCount(mask));
BISHOP_LOOKUP[square][key] = attacks;
}
}
}
This uses plain magic bitboards with fixed size lookup tables, all of length 512 when masks with less set bits could fit in less space. Like on square b1 the mask uses 5 bits on the diagonal and the table could fit in an array of length 2^5 = 32. We're wasting (512 - 32) * (8 bytes per 64 bits number) / 1024 bytes per Kio = 3.75Kio for this square. Plain magic bitboards take 2Mib of memory for rooks and 256Kib for bishops, using fancy magic bitboards can reduce the total to ~800Kib. It's not really needed though, 2.25Mib of memory is small.
I'm writing a method to store a number's prime factor.
I'm asked to use 2-d array to store its prime factor and the factor's number.
public static int[][] getMatrix (long x){
int[][] matrix =new int[10][2];
int count;
for (int i = 2, j = 0; i <=x / 2; i++) {
count=0;
while (x % i == 0) {
x = x/i;
count++;
}
matrix[j][0] = i;
matrix[j][1] = count;
j++;
}
return matrix;
}
But this code only store data into the first row of the array.
Could someone help me correct it or privide other ideas?
if I use the following code to output the result.
for(int row=0;row<b_matrix.length;row++)
{
for(int column=0;column<2;column++)
{
System.out.print(b_matrix[row][column]+" ");
}
}
And x=9 I got this:
2 0 3 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
x=6 I got this:
2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
such as :6
matrix[0][0]=2 matrix[0][1]=1
matrix[1][0]=3 matrix[1][1]=1 //can't store
such as :9
matrix[0][0]=2 matrix[0][1]=0//only output the next row when this equals to 0
matrix[1][0]=3 matrix[1][1]=2
Your logic is right except, in the for loop i should go till x instead of x/2 as follows,
for (int i = 2, j = 0; i <= x; i++)
Output for getMatrix(60):
2 2
3 1
4 0
5 1
0 0
0 0
0 0
0 0
0 0
0 0
I need to print the a ppm picture but the output isn't organized as it should be, my code:
public static int[][][] read(String filename) {
StdIn.setInput(filename);
StdIn.readLine();
int imgW = StdIn.readInt ();
int imgH = StdIn.readInt ();
int[][][] data = new int[imgH][imgW][3];
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
for (int k = 0; k < data[i][j].length; k++) {
data[i][j][k] = StdIn.readInt();
}
}
}
return data;
}
my output:
255 0 0 0 100 0 0 0 0 0 255 0
255 0 0 0 0 255 175 0 0 0 0 0
0 0 0 0 0 0 0 0 15 175 0 0
0 255 0 255 0 0 0 0 0 0 255 255
the correct output: (basically same like a matrix)
0 0 0 100 0 0 0 0 0 255 0 255
0 0 0 0 255 175 0 0 0 0 0 0
0 0 0 0 0 0 0 15 175 0 0 0
255 0 255 0 0 0 0 0 0 255 255 255
Ppm files also lists the maximum value appearing in the file, which is the 255 in the beginning of your output.
You should add an extra StdIn.readInt(); before your loop.
I have a file with some values in it:
11
8
0 0 1 0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 1 0 0 0
0 0 1 1 1 1 1 1 1 0 0
0 1 1 0 1 1 1 0 1 1 0
1 1 1 1 1 1 1 1 1 1 1
1 0 1 1 1 1 1 1 1 0 1
1 0 1 0 0 0 0 0 1 0 1
0 0 0 1 1 0 1 1 0 0 0
I need to read those values into a 2D ArrayList. The fist two values (11 and 8) would be the number of rows and columns respectively. So here is the code:
Scanner scanner = new Scanner(file);
int x, y;
x = scanner.nextInt();
System.out.println(x + " has been read");
y = scanner.nextInt();
System.out.println(y + " has been read");
ArrayList<ArrayList<Boolean>> pixelMap;
pixelMap = new ArrayList<ArrayList<Boolean>>();
ArrayList<Boolean> buffer_line = new ArrayList<Boolean>();
Boolean buffer;
for (int i = 0; i < x; i++){
for (int j = 0; j < y; j++){
buffer = scanner.nextBoolean();
System.out.println(buffer + " has been read");
//buffer_line.add(buffer);
}
//pixelMap.add(buffer_line);
//buffer_line.clear();
}
The problem is - the program reads first two numbers successfully, and when it comes to boolean values, it throws InputMismatch exception on line
buffer = scanner.nextBoolean();
so I can't undersand why. 0 should be read next and it is boolean - so what's actually mismatching?
I also point out that if change buffer type to integer and then assign scanner.nextInt(), the program would read all the values properly, so in the output I would see all of them. So then of course, I can change ArrayList to Integer to make that work, but it would be semantically wrong as it would hold only boolean values.
Can anyone help me to find out the problem?
In your code you have this statement:
buffer = scanner.nextBoolean();
But I do not see boolean values true or false in the input file.
In Java, 0 and 1 are not treated as boolean values like in other languages such as C.
You need to read these values as int and then manually map them to boolean values.
Logic something like this:
int val = scanner.nextInt();
boolean buffer = (val == 1) ? true : false;