kiriloff
kiriloff

Reputation: 26333

Hexadecimal bitwise operations

I am reading very interesting lesson on big and little endianness. A pseudo code sample explains how to convert longInt (4 bytes) from big endian to little endian or the reverse thing.

    Function Reverse (N:LongInt) : LongInt ;
     Var B0, B1, B2, B3 : Byte ;
    Begin
    B0 := (N AND $000000FF) SHR  0 ;
    B1 := (N AND $0000FF00) SHR  8 ;
    B2 := (N AND $00FF0000) SHR 16 ;
    B3 := (N AND $FF000000) SHR 24 ;
    Reverse := (B0 SHL 24) OR (B1 SHL 16) OR (B2 SHL 8) OR (B3 SHL 0) ;
    End ;

The code is like that, and I am in trouble with the from the first line with hexadecimal representation. How do they extract here first byte by doing that

B0 := (N AND $000000FF) SHR  0 ;

? What does operation

 N AND $000000FF

do?

I see what AND does for binary representation, but what about hexadecimal representation? And my amazement is all the same for the next lines.

Thanks and regards.

Upvotes: 0

Views: 1799

Answers (1)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272752

A hexadecimal digit is equivalent to 4 consecutive binary digits, e.g. C <==> 1100. Bitwise operations operate on the equivalent binary representations.

Upvotes: 1

Related Questions