Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Swap (razmena) bitova

[es] :: C/C++ programiranje :: Swap (razmena) bitova

[ Pregleda: 2365 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

Yu Raider
Ivan Djordjevic
Bg

Član broj: 40731
Poruke: 273
*.dynamic.sbb.rs.



+2 Profil

icon Swap (razmena) bitova16.03.2009. u 20:48 - pre 183 meseci
Pozdrav.
Imam promenljivu oblika
Code:

short a = 0x22a2;


Želeo bih da obrnem redosled cifara tako da bude 0xa222, takođe mi je potrebno da izdvojim svaku cifru pojedinačno.
Pretpostavljam da to može da se uradi operacijama sa bitovima ali nisam siguran kako.

Svaka pomoć je dobrodošla
SLIder.
 
Odgovor na temu

jankie88
Igor Janošev
Beograd

Član broj: 22935
Poruke: 27
*.dynamic.sbb.rs.



Profil

icon Re: Swap (razmena) bitova17.03.2009. u 14:15 - pre 183 meseci
Recimo ovako...

Code:

a = ((a << 8) & 0xff00) |      /* dve nize hexa cifre pomerene u levo za 8 bita */
    ((a >> 8) & 0x00ff);       /* dve vise hexa cifre pomerene u desno za 8 bita */  


Samo moras da paziš na veličinu short-a kada ispisuješ promenljivu...
Preporučujem da se tema pomeri u "c za početnike"...
All it takes is one bad day to reduce the sanest man alive to lunacy!
That's how far the world is from where I am: Just one bad day...
 
Odgovor na temu

X Files
Vladimir Stefanovic
Pozarevac

SuperModerator
Član broj: 15100
Poruke: 4902
*.dynamic.sbb.rs.

Jabber: xfiles@elitesecurity.org


+638 Profil

icon Re: Swap (razmena) bitova17.03.2009. u 14:23 - pre 183 meseci
Pogledaj TOP temu: Bit Twiddling Hacks


Swapping individual bits with XOR
http://graphics.stanford.edu/~.../bithacks.html#SwappingBitsXOR
 
Odgovor na temu

Yu Raider
Ivan Djordjevic
Bg

Član broj: 40731
Poruke: 273
*.dynamic.sbb.rs.



+2 Profil

icon Re: Swap (razmena) bitova17.03.2009. u 14:47 - pre 183 meseci
Hvala na odgovorima!

EDIT:

Sledeci kod daje cudan output:
Code:

    opCode = ((opCode << 8) & 0xFF00 |
              (opCode >> 8) & 0x00FF);


Odnosno, output je ffffa222.

Cak i kada je ovako napisano,
Code:

    opCode = ((opCode << 8) & 0xFF00 |
              (opCode >> 8) & 0x00FF) & 0x0000FFFF;

date isti rezultat.

Medjutim, sledeci kod daje ocekivan output (222)
Code:

    opCode = ((opCode << 8) & 0xFF00 |
              (opCode >> 8) & 0x00FF) & 0x00000FFF;


Printovao sam sizeof(short) i output je 2
Gde gresim :/?

[Ovu poruku je menjao Yu Raider dana 17.03.2009. u 16:00 GMT+1]
SLIder.
 
Odgovor na temu

X Files
Vladimir Stefanovic
Pozarevac

SuperModerator
Član broj: 15100
Poruke: 4902
*.dynamic.sbb.rs.

Jabber: xfiles@elitesecurity.org


+638 Profil

icon Re: Swap (razmena) bitova17.03.2009. u 15:04 - pre 183 meseci
HEX(22A2) == BIN(0010 0010 1010 0010)

Citat:

Swapping individual bits with XOR
unsigned int i, j; // positions of bit sequences to swap
unsigned int n; // number of consecutive bits in each sequence
unsigned int b; // bits to swap reside in b
unsigned int r; // bit-swapped result goes here

int x = ((b >> i) ^ (b >> j)) & ((1 << n) - 1); // XOR temporary
r = b ^ ((x << i) | (x << j));
As an example of swapping ranges of bits suppose we have have b = 00101111 (expressed in binary) and we want to swap the n = 3 consecutive bits starting at i = 1 (the second bit from the right) with the 3 consecutive bits starting at j = 5; the result would be r = 11100011 (binary).

This method of swapping is similar to the general purpose XOR swap trick, but intended for operating on individual bits. The variable x stores the result of XORing the pairs of bit values we want to swap, and then the bits are set to the result of themselves XORed with x. Of course, the result is undefined if the sequences overlap.


Code:

// NETESTIRANO !!!
#include <stdio.h>
#include <stdlib.h>

int main()
{
    unsigned int i=1, j=9; // positions of bit sequences to swap
    unsigned int n=8;    // number of consecutive bits in each sequence
    unsigned int b = 0x22A2;    // bits to swap reside in b
    unsigned int r;    // bit-swapped result goes here

    int x = ((b >> i) ^ (b >> j)) & ((1 << n) - 1); // XOR temporary
    r = b ^ ((x << i) | (x << j));

    printf("%x\n", r );

    return 0;
}
 
Odgovor na temu

jankie88
Igor Janošev
Beograd

Član broj: 22935
Poruke: 27
*.dynamic.sbb.rs.



Profil

icon Re: Swap (razmena) bitova18.03.2009. u 00:22 - pre 183 meseci
To se dešava zato što se štampa kao int. Stavi kao format "%hx" i biće sve u redu...
Code:
 
    opCode = ((opCode << 8) & 0xFF00 | (opCode >> 8) & 0x00FF);
    printf("%hx\n", opCode);

All it takes is one bad day to reduce the sanest man alive to lunacy!
That's how far the world is from where I am: Just one bad day...
 
Odgovor na temu

Yu Raider
Ivan Djordjevic
Bg

Član broj: 40731
Poruke: 273
*.dynamic.sbb.rs.



+2 Profil

icon Re: Swap (razmena) bitova18.03.2009. u 15:48 - pre 183 meseci
Hvala puno, uspeo sam na drugi nacin (unsigned short umesto short)

Pozdrav
SLIder.
 
Odgovor na temu

[es] :: C/C++ programiranje :: Swap (razmena) bitova

[ Pregleda: 2365 | Odgovora: 6 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.