+ Post New Thread
Results 1 to 2 of 2
  1. #1
    Junior Member level 1
    Points: 224, Level: 2

    Join Date
    Aug 2018
    Posts
    17
    Helped
    0 / 0
    Points
    224
    Level
    2

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

    Hi ,

    can anyone explain me the logic of this IDIOM

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

    •   AltAdvertisement

        
       

  2. #2
    Advanced Member level 4
    Points: 5,647, Level: 17
    kripacharya's Avatar
    Join Date
    Dec 2012
    Location
    New Delhi
    Posts
    1,026
    Helped
    173 / 173
    Points
    5,647
    Level
    17

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

    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


    1 members found this post helpful.

--[[ ]]--