I
I
Im p3l2018-04-03 20:06:57
assembler
Im p3l, 2018-04-03 20:06:57

How to swap bits in a byte?

Good day, I ask for help.
It is necessary to swap the bits in the number 120 (0 and 1, 2 and 3, 4 and 7, 5 and 6).
I really don't see how it can be done.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
awesomer, 2018-04-03
@awesomer

  1. even mask, original number AND 01010101
    mask of course in the binary system is given.
    figured it out in 2 seconds, I could be wrong.

J
jcmvbkbc, 2018-04-03
@jcmvbkbc

You can, for example, create a new number in which the necessary bits of the old number will be in the right places.
What is the difficulty, in the algorithm or in the implementation?

F
freeExec, 2018-04-03
@freeExec

Get the value of bit 4:

mov eax, 1
shl eax, 4
mov ecx, 120
and ecx, eax
shr ecx, 4

Set bit 4
mov eax, 1
shl eax, 4
mov ecx, 120
or ecx, eax

Reset bit 4
mov eax, 1
shl eax, 4
not eax
mov ecx, 120
and ecx, eax

Naturally, if you know everything in advance, you can hardcode the masks. This is to solve the problem in the forehead. And so read about arithmetic shifts, when you can control which bit is dropped / inserted.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question