[ Yu Raider @ 16.03.2009. 20:48 ] @
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
[ jankie88 @ 17.03.2009. 14:15 ] @
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"...
[ X Files @ 17.03.2009. 14:23 ] @
Pogledaj TOP temu: Bit Twiddling Hacks


Swapping individual bits with XOR
http://graphics.stanford.edu/~.../bithacks.html#SwappingBitsXOR
[ Yu Raider @ 17.03.2009. 14:47 ] @
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]
[ X Files @ 17.03.2009. 15:04 ] @
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;
}
[ jankie88 @ 18.03.2009. 00:22 ] @
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);
[ Yu Raider @ 18.03.2009. 15:48 ] @
Hvala puno, uspeo sam na drugi nacin (unsigned short umesto short)

Pozdrav