- https://www.codewars.com/kata/526571aae218b8ee490006f4
- My solution
- Bitwise operators
- Bitwise calculator
This kata is about counting bits 1 on an integer.
GCC has a __builtin__popcount(int)
which I could use but I didn't know about it.
- I used a for loop to iterate over the bits of the integer (32 bits).
- In each iteration I bit shifted the integer to the right by
i
bits and then I used the&
operator with1
to check if the lower bit of the integer is 1. - If it is, I increment the counter.
Another alternative: https://stackoverflow.com/a/51388846
#include <stddef.h>
size_t countBits(unsigned n)
{
n = (n & 0x55555555u) + ((n >> 1) & 0x55555555u);
n = (n & 0x33333333u) + ((n >> 2) & 0x33333333u);
n = (n & 0x0f0f0f0fu) + ((n >> 4) & 0x0f0f0f0fu);
n = (n & 0x00ff00ffu) + ((n >> 8) & 0x00ff00ffu);
n = (n & 0x0000ffffu) + ((n >>16) & 0x0000ffffu);
return n;
}
- Characters can be casted as integers following the ASCII table.
- Char* can be iterated as an array, and end with a null terminator (\0 or 0 in decimal).
- I used a for loop to iterate over the mini_wins of the ticket.
- Read mini_win letter and cast it to an integer.
- If the char as an integer is equal to the win number, increment the counter.
- If the counter is equal to the mini_win size, return "Winner!".
- If the counter is not equal to the mini_win size, return "Loser!".
Have some problemas with this kata, it seems it has some bugs in the tests.
TODO
https://www.codewars.com/kata/545cdb4f61778e52810003a2/train/c
gcc -o toc -lcurl toc.c && chmod +x toc && ./toc
- https://conclase.net/c/librerias/stdio/ftell
- https://cdiv.sourceforge.net/cdivhlp/const_SEEK.htm
- https://learn.microsoft.com/es-es/cpp/c-runtime-library/reference/fgetc-fgetwc?view=msvc-170
- https://www.ibm.com/docs/es/i/7.5?topic=functions-strcat-concatenate-strings
- https://www.ibm.com/docs/es/i/7.5?topic=functions-fprintf-write-formatted-data-stream
- Repeating a character in printf
- asnprintf
- Curl escape url util