To swap bytes 0 and 1, and 2 and 3 of a 32-bit word, use the idiom as

Status
Not open for further replies.

sushl

Junior Member level 2
Joined
Aug 22, 2018
Messages
24
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
238
Hi ,

can anyone explain me the logic of this IDIOM

(((x&0xff)<<8)¦((x&0xff00)>>8)¦((x&0xff000000)>>8)¦((x&0x00ff0000)<<8));
 

It swaps byte 0<-->1, and byte 2<-->3.

The '&0ff' is basically extracting specific sets of 8 bits at a time. Think A and 011111111

Next, the '<<' or '>>' operators then shift left or right by 8 bits.

And of course the '|' or operator combines them back again.

Quite simple really
 
Reactions: sushl

    sushl

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…