K
K
Konstantin Malyarov2022-01-27 21:25:59
C++ / C#
Konstantin Malyarov, 2022-01-27 21:25:59

How to calculate int bit by bit in c++?

There is an ESP8266 that needs to be sent bit by bit 0xF0:
1111 0000, first send 1, then 1, then 1, and so on.
How to implement? I wanted to shift the bit to the left, but did not understand how to implement it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2022-01-27
@Konstantin18ko

To take the i-th bit from the right, you need to shift the number i positions to the right and apply a mask with one unit at the end through a bitwise AND:
(x >> i) & 1
You need to loop through the bits from the highest (7, if one byte) to the lowest with number 0 and take construction above.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question