misc:software:AutoHotkey:自作スクリプト:bit演算の拡張

misc:software:AutoHotkey:自作スクリプト:bit演算の拡張

result := getStringBitwiseNot("101011100010101010101100010101010101100010101010101100010101010101010101010010101101011100")
msgbox,% convertNumArrayToString(result)



getStringBitAnd(aLeftString, aRightString)
{
    return operateStringBit(aLeftString, aRightString, "logicalAnd")
}

getStringBitOr(aLeftString, aRightString)
{
    return operateStringBit(aLeftString, aRightString, "logicalOr")
}

getStringBitXor(aLeftString, aRightString)
{
    return operateStringBit(aLeftString, aRightString, "logicalXor")
}

getStringBitNot(aLeftString)
{
    stringForBitNot := makeString("1", strLen(aLeftString)) 
    return operateStringBit(aLeftString, stringForBitNot, "logicalXor")
}


logicalAnd(aLeft, aRight){
    return aLeft & aRight
}

logicalOr(aLeft, aRight){
    return aLeft | aRight
}

logicalXor(aLeft, aRight){
    return aLeft ^ aRight
}


operateStringBit(ByRef aLeftString, ByRef aRightString, operator)
{
    results := convertStringsToNumArrays(aLeftString, aRightString)
    leftNumArray := getLeftNumArray(results)
    rightNumArray := getRightNumArray(results)

    Loop, % leftNumArray.length()
    {
        leftNumArray[A_Index] := %operator%(leftNumArray[A_Index], rightNumArray[A_Index])
    }
    
    return convertNumArrayToString(leftNumArray)
}

convertNumArrayToString(ByRef aArray)
{
    result := []
    arr := aArray.clone()
      
    loop, % arr.length()
    {
       result.push(arr.pop())
    }
    
    return convertArrayToString(result)
}

convertArrayToString(ByRef aArray, aJoin := "")
{
    result := ""
    for _, value in aArray
    {
        result .= value . aJoin
    }
    
    return result
}

getLeftNumArray(ByRef aArray)
{
    return aArray[1]
}

getRightNumArray(ByRef aArray)
{
    return aArray[2]
}

convertStringsToNumArrays(ByRef aLeftString, ByRef aRightString)
{
    leftNumArray  := convertStringToNumArray(aLeftString)
    rightNumArray := convertStringToNumArray(aRightString)
    
    diffIndex := leftNumArray.MaxIndex() - rightNumArray.MaxIndex()
    
    if (0 < diffIndex ) {
        theArray := rightNumArray
    } else {
        theArray := leftNumArray
    }
    
    Loop, % abs(diffIndex)
    {
        theArray.push(0)
    }

    return [leftNumArray, rightNumArray]
}

convertStringToNumArray(ByRef aStr)
{
    arr := []
    
    Loop, % strLen(aStr)
    {
        arr.push(convertStringToNumber(StringRight(aStr, 1)))
        aStr := StringTrimRight(aStr, 1)
    }
    
    return arr
}

convertStringToNumber(ByRef aStr)
{
    return aStr + 0
}

;stringRight(ByRef aInputVar, aCount) {
;    StringRight, outputVar, aInputVar, aCount
;    return outputVar
;}

;stringTrimRight(ByRef aInputVar, aCount) {
;    StringTrimRight, aInputVar, aInputVar, aCount
;    return aInputVar
;}

makeXBitString(aCount)
{
    return "1" . makeString("0", aCount)
}

makeString(aString, aCount)
{
    result .= ""
    loop, % aCount
    {
        result .= aString
    }
    
    return result
}

Last modified : 2017/01/22 20:05:05 JST
blechmusik (blechmusik@gmail.com)