Milionerzy: najlepsza kolejność wykorzystania kół ratunkowych

Grasz w milionerów, masz wszystkie koła ratunkowe i nagle dostajesz makabryczne pytanie. Nie wiesz. Bierzesz koła. Najpierw 50:50, a później publiczność, czy najpierw publiczność, a potem 50:50? Ta dyskusja rozgorzała na joemosterze, wiec i ja postanowiłem się temu przyjrzeć. Warunki początkowe Zakładam,...

How to distinguish human languages by letter frequency histogram?

How to find without dictionaries a language of a text sample? It can be accomplished by comparing frequencies of letters in a sample and in known languages. For example in polish 'a' letter is about 0.0551146 of all letters, in french it's 0.049458 and in german 0.0434701. I created a small program...

A simple trick to avoid "goto" statement

Error handling in C is sometimes done by using goto and labels. One of examples in the Linux kernel: static struct avc_node *avc_alloc_node(void) { struct avc_node *node; node = kmem_cache_zalloc(avc_node_cachep, GFP_ATOMIC); if (!node) goto out; INIT_RCU_HEAD(&node->rhead); ...

Easier error handling by using automatic code generation [Perl interpreter]

Perl interpreter has an interesting mechanism to checks if function arguments have correct values. Below is its example from util.c: char * Perl_delimcpy(register char *to, register const char *toend, register const char *from, register const char *fromend, register int...

Remote control of devices by using PC Speaker

This is one of my older projects that aims to control external devices directly, by using the PC Speaker port. It can control multiple devices at one go, but they must be powered one at a time. I made it a couple of years ago and it was working fine, but due to mistake during plugging it literally burned...

Czym jest medytacja? Jak medytować? Popularne techniki medytacji.

W codziennym życiu koncentrujemy się na otaczającym nas świecie, gdy jesteśmy w pracy zajmujemy sie pracą, gdy jedziemy autobusem patrzymy na świat za oknem, a gdy rozmawiamy z kimś, to słuchamy go i współuczestniczymy w rozmowie. To normalne zachowanie, dzięki któremu możemy utrzymywać się materialnie,...

How to use finite state machine in parsing of assembly language?

Finite State Machine - abstract Often data must be analyzed chunk by chunk, checked if all of those chunks are valid (if the chunk is valid itself and if it's valid in context of previous chunks) and when some actions must be taken according to each type of this chunks. This can be easily modeled using...

How to use regular diode as a photo-element?

There are special diodes, transistors and resistors designed to measure light intensity, sometimes even a regular LED is also used for this task, can be used. I was wondering, if any diode (in a package that doesn't block light rays) could be used? Today I will share the results of my experiments...

Mistakes during checking if a returned value is null

This is a bit modified real word example, found by tool for static analysis. In this code, foo() is a function that returns pointer to std::string or null pointer. std::cout << foo()->length() << std::endl; There's obvious bug here: executing length() method on value returned by foo()...

try/catch in C [PHP interpreter]

Abstract PHP interpreter is written in C, but uses try/catch construct known from other popular languages. This approach is different from everyday C-like error handling, where it's done by using function return value or by modifying error variable passed as an argument to a function. zend_try...

Access to object's fields by using pointer arithmetic

Normally a method uses its object's field by using their name. It's presented below, where we want to get value of description field from getDescription method. class Car { std::string getDescription() { return description; // <- we return value of this field } std::string description; }; The...

Images hardcoded in a source code [PHP interpreter]

Abstract Recently I presented a simple way to protect an image against saving, today I will show how PHP interpreter protect some of its images against changing. Image may be hardcoded in a regular array, where each field contains corresponding value of a byte in the original image file. You may imagine...

Smarter strings in C

In C, strings are usually stored in variables with char* type, it's just container for characters, there isn't stored other data/metadata. This can be tedious, e.g. if we want to iterate thought string we need to obtain its length (e.g by invoke strlen() function on it) or carry its length in some additional...

How to replace modulo operator by using bitwise AND operator?

Modulo operation on a and b returns a reminder from dividing a by b. It can be implemented by using bitwise AND operator if b is a power of two. Following samples in C and Python illustrate it. #include <stdio.h> int main() { // 8(dec) = 1000(bin) // 7(dec) = 111(bin) printf("19...

HOWTO avoid mistakes in conditional instructions

In C or C++ it's easy to made mistake in expression of conditional instruction and use assignment instead of comparison. It's presented in below snippet, where we want to check if x is equal to 6: int main(){ int x; /* first case */ /* OK */ if(x==6){ /* sth */ } /*...

Protecting images against saving

If you share on the Internet your images, then you may want to protect them against saving. There're a lot of ways to do it - watermarks, cutting images into multiple smaller pieces and reassembling them by using CSS, tricks with CSS or JavaScript and others. None of them is accurate against experienced...

Less popular way to copy content of a structure

There is instance (let's call it p0) of structure, we create new instance (p1) of the same structure and want to copy content of fields from p0 to p1. The simplest (IMHO also the best) way is to copy field by field. It's presented in following code (in case of p1 all fields are copied, in case of p2...