memesearch
Идеальный логарифм из костылей

Идеальный логарифм из костылей

Download Find similar pictures
Text on the meme
#include <stdio.h> int log2(unsigned n) { union { unsigned ui[2]; double d; } u = {.d=n + 0.5}; return 8*sizeof (unsigned) - ((1054 - (u.ui[1] >> 20)) -1; } int main() { printf("%d\n", log2(2)); printf("%d\n", log2(4)); printf("%d\n", log2(8)); printf("%d\n", log2(16)); printf("%d\n", log2(32)); printf("%d\n", log2(64)); printf("%d\n", log2(128)); } ASM generation compiler returned: 0 Execution build compiler returned: 0 Program returned: 0 1 2 3 4 5 6 7
What it means

На скриншоте показана программа на C, которая вычисляет логарифм по основанию 2 с помощью низкоуровневого трюка с представлением числа double в памяти и union. Код выглядит совершенно непонятно, но компилятор успешно собирает его, а программа выдаёт правильные результаты от 1 до 7. Мем отправляют, когда хочется понимать, как что-то работает, но лучше не вникать в пугающую магию кода. EN: The screenshot shows a C program that calculates the base-2 logarithm using a low-level trick involving the in-memory representation of a double and a union. The code looks completely incomprehensible, yet the compiler builds it successfully and the program produces the correct results from 1 to 7. This meme is used when you want to understand how something works, but would rather not dig into frightening code magic.

Objects
Tags
Source
Type
image
Size
557×800 · 55.6 KiB
Mood
саркастичный, абсурдный
Language
ru
Added