How to perform a bitwise comparison of variables that do not have the same size / type

I am currently doing a small task to solve problems in C, so here is my problem: I get a console program that receives Tetris elements in standard input, for example,

.#..
.#..
.#..
.#..

..#.
###.
....
....

....
.##.
.##.
....

etc... (max 26 pieces)

and I have to figure out how to make them fit in the smallest possible square (without rotation) and in the very top left corner in order to deduce the following for the previous example:

ABB.
ABBC
ACCC
A...

(where letters are assigned in order of appearance).

So, I decided that the fastest way for my computer to check whether a piece is suitable in a spot on the map is to perform bitwise execution &. If it gives 0, it fits.

That way I can check if

1100
1100
0000
0000

fit:

1000
1000
1000
1000

if (a & b == 0), , . , , :

b >> 1 = 
0110
0110
0000
0000

, , .

, short int (16 ), (, long long, 8x8) 0 :

0110
0110
0000
0000

to

0000
0000
0011
0011

, :

00000000
00110000
00110000
00000000
00000000
00000000
00000000
00000000

64- int 1 0.

+4
2

@user694733, :

uint64_t pieceToMap(uint16_t little) {
  static uint16_t row = 0b1111;
  uint64_t big = 0;

  for (char i = 0; i < 16; i += 4) {
    big |= (little & (row << i)) << i;
  }

  return big;
}

:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

uint64_t pieceToMap(uint16_t);
void printPiece(uint16_t);
void printMap(uint64_t);

int main(int argc, char *argv[]) {
  uint16_t a = 0b0110011000000000;
  uint64_t b = pieceToMap(a);
  uint64_t c = b << 36;

  printPiece(a);
  printf("\r\n");
  printMap(b);
  printf("\r\n");
  printMap(c);

  return EXIT_SUCCESS;
}

uint64_t pieceToMap(uint16_t little) {
  static uint16_t row = 0b1111;
  uint64_t big = 0;

  for (char i = 0; i < 16; i += 4) {
    big |= (little & (row << i)) << i;
  }

  return big;
}

void printPiece(uint16_t little) {
  static uint16_t i = 1;
  char row, col;

  for (row = 3; row >= 0; row--) {
    for (col = 3; col >= 0; col--) {
      printf("%c", (little & (i << (row * 4 + col))) ? '1' : '0');
    }

    printf("\r\n");
  }
}

void printMap(uint64_t big) {
  static uint64_t i = 1;
  char row, col;

  for (row = 7; row >= 0; row--) {
    for (col = 7; col >= 0; col--) {
      printf("%c", (big & (i << (row * 8 + col))) ? '1' : '0');
    }

    printf("\r\n");
  }
}

, , :

0110
0110
0000
0000

00000000
00000000
00000000
00000000
00000110
00000110
00000000
00000000

01100000
01100000
00000000
00000000
00000000
00000000
00000000
00000000
+2

(short int) , int. , :

, . , .

, - , . , uintN_t stdint.h.

, - :

// bool type is from stdbool.h
bool isPieceInMap(uint16_t piece, uint64_t map) {
    uint64_t bigPiece = piece;
    // Fix lines
    bigPiece = 
        (bigPiece & 0x000F) |
        (bigPiece & 0x00F0) << 4 |
        (bigPiece & 0x0F00) << 8 |
        (bigPiece & 0xF000) << 12;
    bigPiece = // Do position shifting as needed
    return (bigPiece & map) == 0;
}
+3

Source: https://habr.com/ru/post/1623689/


All Articles