• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Snippet] Byte And

This is just like the bitwise "Byte Xor" library by Nestharus.

It will be useful for writing hashing algorithms.

JASS:
/***************************
*
*   SubAnd
*   v1.0.0.1
*   By Magtheridon96
*
*   - Lookup table for sub-byte bitwise
*     AND operations.
*
*   Explanation:
*   ------------
*
*       The library has an array that stores all possible 
*       sub-byte (4-bits) results for a bitwise & operation.
*
*       data[0x3e] is used to store the result of (0x3 & 0xe).
*       data[0x4a] is used to store the result of (0x4 & 0xa).
*
*   API:
*   ----
*
*       constant function S_AND takes integer subByte1, integer subByte2 returns integer
*           - Returns (subByte1 & subByte2)
*           - A sub-byte is in the range [0x0 ... 0xf].
*
***************************/
library SubAnd
    
    globals
        private integer array data
    endglobals
    
    private module andm
        private static method onInit takes nothing returns nothing
            set data[0x00] = 0x0
            set data[0x01] = 0x0
            set data[0x02] = 0x0    
            set data[0x03] = 0x0    
            set data[0x04] = 0x0    
            set data[0x05] = 0x0    
            set data[0x06] = 0x0    
            set data[0x07] = 0x0    
            set data[0x08] = 0x0    
            set data[0x09] = 0x0    
            set data[0x0a] = 0x0    
            set data[0x0b] = 0x0    
            set data[0x0c] = 0x0    
            set data[0x0d] = 0x0    
            set data[0x0e] = 0x0    
            set data[0x0f] = 0x0
            set data[0x10] = 0x0
            set data[0x11] = 0x1
            set data[0x12] = 0x0
            set data[0x13] = 0x1
            set data[0x14] = 0x0
            set data[0x15] = 0x1
            set data[0x16] = 0x0
            set data[0x17] = 0x1
            set data[0x18] = 0x0
            set data[0x19] = 0x1
            set data[0x1a] = 0x0
            set data[0x1b] = 0x1
            set data[0x1c] = 0x0
            set data[0x1d] = 0x1
            set data[0x1e] = 0x0
            set data[0x1f] = 0x1
            set data[0x20] = 0x0
            set data[0x21] = 0x0
            set data[0x22] = 0x2
            set data[0x23] = 0x2
            set data[0x24] = 0x0
            set data[0x25] = 0x0
            set data[0x26] = 0x2
            set data[0x27] = 0x2
            set data[0x28] = 0x0
            set data[0x29] = 0x0
            set data[0x2a] = 0x2
            set data[0x2b] = 0x2
            set data[0x2c] = 0x0
            set data[0x2d] = 0x0
            set data[0x2e] = 0x2
            set data[0x2f] = 0x2
            set data[0x30] = 0x0
            set data[0x31] = 0x1
            set data[0x32] = 0x2
            set data[0x33] = 0x3
            set data[0x34] = 0x0
            set data[0x35] = 0x1
            set data[0x36] = 0x2
            set data[0x37] = 0x3
            set data[0x38] = 0x0
            set data[0x39] = 0x1
            set data[0x3a] = 0x2
            set data[0x3b] = 0x3
            set data[0x3c] = 0x0
            set data[0x3d] = 0x1
            set data[0x3e] = 0x2
            set data[0x3f] = 0x3
            set data[0x40] = 0x0
            set data[0x41] = 0x0
            set data[0x42] = 0x0
            set data[0x43] = 0x0
            set data[0x44] = 0x4
            set data[0x45] = 0x4
            set data[0x46] = 0x4
            set data[0x47] = 0x4
            set data[0x48] = 0x0
            set data[0x49] = 0x0
            set data[0x4a] = 0x0
            set data[0x4b] = 0x0
            set data[0x4c] = 0x4
            set data[0x4d] = 0x4
            set data[0x4e] = 0x4
            set data[0x4f] = 0x4
            set data[0x50] = 0x0
            set data[0x51] = 0x1
            set data[0x52] = 0x0
            set data[0x53] = 0x1
            set data[0x54] = 0x4
            set data[0x55] = 0x5
            set data[0x56] = 0x4
            set data[0x57] = 0x5
            set data[0x58] = 0x0
            set data[0x59] = 0x1
            set data[0x5a] = 0x0
            set data[0x5b] = 0x1
            set data[0x5c] = 0x4
            set data[0x5d] = 0x5
            set data[0x5e] = 0x4
            set data[0x5f] = 0x5
            set data[0x60] = 0x0
            set data[0x61] = 0x0
            set data[0x62] = 0x2
            set data[0x63] = 0x2
            set data[0x64] = 0x4
            set data[0x65] = 0x4
            set data[0x66] = 0x6
            set data[0x67] = 0x6
            set data[0x68] = 0x0
            set data[0x69] = 0x0
            set data[0x6a] = 0x2
            set data[0x6b] = 0x2
            set data[0x6c] = 0x4
            set data[0x6d] = 0x4
            set data[0x6e] = 0x6
            set data[0x6f] = 0x6
            set data[0x70] = 0x0
            set data[0x71] = 0x1
            set data[0x72] = 0x2
            set data[0x73] = 0x3
            set data[0x74] = 0x4
            set data[0x75] = 0x5
            set data[0x76] = 0x6
            set data[0x77] = 0x7
            set data[0x78] = 0x0
            set data[0x79] = 0x1
            set data[0x7a] = 0x2
            set data[0x7b] = 0x3
            set data[0x7c] = 0x4
            set data[0x7d] = 0x5
            set data[0x7e] = 0x6
            set data[0x7f] = 0x7
            set data[0x80] = 0x0
            set data[0x81] = 0x0
            set data[0x82] = 0x0
            set data[0x83] = 0x0
            set data[0x84] = 0x0
            set data[0x85] = 0x0
            set data[0x86] = 0x0
            set data[0x87] = 0x0
            set data[0x88] = 0x8
            set data[0x89] = 0x8
            set data[0x8a] = 0x8
            set data[0x8b] = 0x8
            set data[0x8c] = 0x8
            set data[0x8d] = 0x8
            set data[0x8e] = 0x8
            set data[0x8f] = 0x8
            set data[0x90] = 0x0
            set data[0x91] = 0x1
            set data[0x92] = 0x0
            set data[0x93] = 0x1
            set data[0x94] = 0x0
            set data[0x95] = 0x1
            set data[0x96] = 0x0
            set data[0x97] = 0x1
            set data[0x98] = 0x8
            set data[0x99] = 0x9
            set data[0x9a] = 0x8
            set data[0x9b] = 0x9
            set data[0x9c] = 0x8
            set data[0x9d] = 0x9
            set data[0x9e] = 0x8
            set data[0x9f] = 0x9
            set data[0xa0] = 0x0
            set data[0xa1] = 0x0
            set data[0xa2] = 0x2
            set data[0xa3] = 0x2
            set data[0xa4] = 0x0
            set data[0xa5] = 0x0
            set data[0xa6] = 0x2
            set data[0xa7] = 0x2
            set data[0xa8] = 0x8
            set data[0xa9] = 0x8
            set data[0xaa] = 0xa
            set data[0xab] = 0xa
            set data[0xac] = 0x8
            set data[0xad] = 0x8
            set data[0xae] = 0xa
            set data[0xaf] = 0xa
            set data[0xb0] = 0x0
            set data[0xb1] = 0x1
            set data[0xb2] = 0x2
            set data[0xb3] = 0x3
            set data[0xb4] = 0x0
            set data[0xb5] = 0x1
            set data[0xb6] = 0x2
            set data[0xb7] = 0x3
            set data[0xb8] = 0x8
            set data[0xb9] = 0x9
            set data[0xba] = 0xa
            set data[0xbb] = 0xb
            set data[0xbc] = 0x8
            set data[0xbd] = 0x9
            set data[0xbe] = 0xa
            set data[0xbf] = 0xb
            set data[0xc0] = 0x0
            set data[0xc1] = 0x0
            set data[0xc2] = 0x0
            set data[0xc3] = 0x0
            set data[0xc4] = 0x4
            set data[0xc5] = 0x4
            set data[0xc6] = 0x4
            set data[0xc7] = 0x4
            set data[0xc8] = 0x8
            set data[0xc9] = 0x8
            set data[0xca] = 0x8
            set data[0xcb] = 0x8
            set data[0xcc] = 0xc
            set data[0xcd] = 0xc
            set data[0xce] = 0xc
            set data[0xcf] = 0xc
            set data[0xd0] = 0x0
            set data[0xd1] = 0x1
            set data[0xd2] = 0x0
            set data[0xd3] = 0x1
            set data[0xd4] = 0x4
            set data[0xd5] = 0x5
            set data[0xd6] = 0x4
            set data[0xd7] = 0x5
            set data[0xd8] = 0x8
            set data[0xd9] = 0x9
            set data[0xda] = 0x8
            set data[0xdb] = 0x9
            set data[0xdc] = 0xc
            set data[0xdd] = 0xd
            set data[0xde] = 0xc
            set data[0xdf] = 0xd
            set data[0xe0] = 0x0
            set data[0xe1] = 0x0
            set data[0xe2] = 0x2
            set data[0xe3] = 0x2
            set data[0xe4] = 0x4
            set data[0xe5] = 0x4
            set data[0xe6] = 0x6
            set data[0xe7] = 0x6
            set data[0xe8] = 0x8
            set data[0xe9] = 0x8
            set data[0xea] = 0xa
            set data[0xeb] = 0xa
            set data[0xec] = 0xc
            set data[0xed] = 0xc
            set data[0xee] = 0xe
            set data[0xef] = 0xe
            set data[0xf0] = 0x0
            set data[0xf1] = 0x1
            set data[0xf2] = 0x2
            set data[0xf3] = 0x3
            set data[0xf4] = 0x4
            set data[0xf5] = 0x5
            set data[0xf6] = 0x6
            set data[0xf7] = 0x7
            set data[0xf8] = 0x8
            set data[0xf9] = 0x9
            set data[0xfa] = 0xa
            set data[0xfb] = 0xb
            set data[0xfc] = 0xc
            set data[0xfd] = 0xd
            set data[0xfe] = 0xe
            set data[0xff] = 0xf
        endmethod
    endmodule
    
    private struct ands extends array
        implement andm
    endstruct
    
    constant function S_AND takes integer sub1, integer sub2 returns integer
        return data[sub1 * 0x10 + sub2]
    endfunction
    
endlibrary

JASS:
/***************************
*
*   ByteAnd
*   v1.0.0.1
*   By Magtheridon96
*
*   - Used to perform the bitwise AND 
*     operation for 8 bits. (1 byte)
*
*   Requires:
*   ---------
*
*       - SubByte by Nestharus
*           hiveworkshop.com/forums/jass-resources-412/snippet-sub-byte-222641/
*
*   API:
*   ----
*
*       constant function B_AND takes integer byte1, integer byte2 returns integer
*           - Returns (byte1 & byte2)
*           - The arguments must be in the range [0x00 ... 0xff]
*
***************************/
library ByteAnd requires sbyte, SubAnd

    constant function B_AND takes integer byte1, integer byte2 returns integer
        return S_AND(lbyte[byte1], lbyte[byte2]) * 0x10 + S_AND(rbyte[byte1], rbyte[byte2])
    endfunction

endlibrary

JASS:
/*****************************************
*
*   And32
*   v1.0.0.1
*   By Magtheridon96
*
*   - Provides a bitwise AND function 
*     that works for integers. (32-bits)
*
*   Credits:
*   --------
*
*       - Nestharus for the update.
*
*   API:
*   ----
*
*       function AND32 takes integer int1, integer int2 returns integer
*           - Returns int1 & int2
*
*****************************************/
library And32 requires ByteAnd
    globals
        private integer b1
        private integer b2
    endglobals
    function AND32 takes integer int1, integer int2 returns integer
        if (0 > int1) then
            set int1 = -2147483648 + int1
            set b1 = 1
        else
            set b1 = 0
        endif
        if (0 > int2) then
            set int2 = -2147483648 + int2
            set b2 = 1
        else
            set b2 = 0
        endif
        
        return B_AND(b1*128 + int1/0x1000000, b2*128 + int2/0x1000000)*0x1000000 + /*
            */ B_AND((int1 - int1/0x1000000*0x1000000)/0x10000, (int2 - int2/0x1000000*0x1000000)/0x10000) * 0x10000 + /*
            */ B_AND((int1 - int1/0x10000*0x10000)/0x100, (int2 - int2/0x10000*0x10000)/0x100) * 0x100 + /*
            */ B_AND(int1 - int1/0x100*0x100, int2 - int2/0x100*0x100)
    endfunction
    
endlibrary

Both of these libraries are coupled. They are used for the same purpose, but I'm splitting them for modularity and clarity.

Feel free to comment.
 
Last edited:
Top