> For the complete documentation index, see [llms.txt](https://mentebinaria.gitbook.io/manual-da-linguagem-gnu-c/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mentebinaria.gitbook.io/manual-da-linguagem-gnu-c/6.-aritmetica/operacoes-de-deslocamento/hacks-com-deslocamento.md).

# Hacks com Deslocamento

Você pode usar os operadores de deslocamento para diversos hacks (soluções engenhosas) úteis. Por exemplo, dada uma data especificada pelo dia do mês `d`, mês `m` e ano `y`, você pode armazenar toda a data em um único inteiro `date`:

```c
unsigned int d = 12;        /* 12 em binário é 0b1100.  */
unsigned int m = 6;         /* 6 em binário é 0b110.  */
unsigned int y = 1983;      /* 1983 em binário é 0b11110111111.  */
unsigned int date = (((y << 4) + m) << 5) + d;
/* Adiciona 0b11110111111000000000
   e 0b11000000 e 0b1100.
   A soma é 0b11110111111011001100.  */
```

Para extrair o dia, mês e ano de `date`, use uma combinação de deslocamento e resto:

```c
/* 32 em binário é 0b100000.  */
/* O resto da divisão por 32 dá os 5 bits menos significativos, 0b1100.  */
d = date % 32;
/* Deslocar 5 bits para a direita descarta o dia, restando 0b111101111110110.
   O resto da divisão por 16 dá os 4 bits menos significativos restantes, 0b110.  */
m = (date >> 5) % 16;
/* Deslocar 9 bits para a direita descarta dia e mês,
   restando 0b111101111110.  */
y = date >> 9;
```

`-1 << LOWBITS` é uma maneira inteligente de criar um inteiro cujos `LOWBITS` bits menos significativos são todos 0 e o restante são todos 1. `-(1 << LOWBITS)` é equivalente a isso, devido à associatividade da multiplicação, já que negar um valor é equivalente a multiplicá-lo por -1.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mentebinaria.gitbook.io/manual-da-linguagem-gnu-c/6.-aritmetica/operacoes-de-deslocamento/hacks-com-deslocamento.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
