与powershell的Hamming距离比较

与powershell的Hamming距离比较,powershell,Powershell,如何将8位与另一个8位进行比较,以获得如下内容: first Nr 1 0 0 0 1 1 0 1 second Nr 1 0 1 0 1 0 0 0 Result r r f r r f r f r = right f = false 谢谢您可以使用: e、 g 对于您的场景,您正在查看-bxor功能,只获取结果的负数(-bnot) 如果需要查看标志本身,可以使用以下方法: function Format-BooleanString { [Cmdlet

如何将8位与另一个8位进行比较,以获得如下内容:

first Nr     1 0 0 0 1 1 0 1
second Nr    1 0 1 0 1 0 0 0
Result       r r f r r f r f

r = right 
f = false
谢谢

您可以使用:

e、 g

对于您的场景,您正在查看
-bxor
功能,只获取结果的负数(
-bnot


如果需要查看标志本身,可以使用以下方法:

function Format-BooleanString {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [byte]$ByteToDisplay 
    )
    Process {
        [string]$result = ''
        for ([int]$i = 7; $i -ge 0; $i--) {
            [int]$x = [Math]::Pow(2, $i)
            if ($ByteToDisplay -band $x) {
                $result += '1'
            } else {
                $result += '0'
            }
        }
        $result
    }
}
function Convert-BooleanStringToByte {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string]$ByteAsString
    )
    Process {
        if ($ByteAsString -notmatch '^[01]{8}$') {
            throw 'Input string must be 8 chars, consisting of only 0s and/or 1s'
        }
        [int]$result = 0
        for ([int]$i = 0; $i -lt 8; $i++) {
            if ($ByteAsString[$i] -eq '1') {
                $result += [Math]::Pow(2, 7-$i)
            }
        }
        [byte]$result            
    }

}

Format-BooleanString -ByteToDisplay 9
Convert-BooleanStringToByte -ByteAsString '00100010'
您可以使用:

e、 g

对于您的场景,您正在查看
-bxor
功能,只获取结果的负数(
-bnot


如果需要查看标志本身,可以使用以下方法:

function Format-BooleanString {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [byte]$ByteToDisplay 
    )
    Process {
        [string]$result = ''
        for ([int]$i = 7; $i -ge 0; $i--) {
            [int]$x = [Math]::Pow(2, $i)
            if ($ByteToDisplay -band $x) {
                $result += '1'
            } else {
                $result += '0'
            }
        }
        $result
    }
}
function Convert-BooleanStringToByte {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [string]$ByteAsString
    )
    Process {
        if ($ByteAsString -notmatch '^[01]{8}$') {
            throw 'Input string must be 8 chars, consisting of only 0s and/or 1s'
        }
        [int]$result = 0
        for ([int]$i = 0; $i -lt 8; $i++) {
            if ($ByteAsString[$i] -eq '1') {
                $result += [Math]::Pow(2, 7-$i)
            }
        }
        [byte]$result            
    }

}

Format-BooleanString -ByteToDisplay 9
Convert-BooleanStringToByte -ByteAsString '00100010'
带注释的解决方案:

$firstNrBin  = '1 0 0 0 1 1 0 1'
$secondNrBin = '1 0 1 0 1 0 0 0'

# Convert the binary number strings to [byte]s (unsigned 8-bit values).
$firstNr = [Convert]::ToByte($firstNrBin -replace ' ', 2)   # 141 == 0x8d
$secondNr = [Convert]::ToByte($secondNrBin -replace ' ', 2) # 168 == 0xa8

# Perform bitwise XOR logic and negate the result to get a bit field
# that reflects the bits where the input numbers match.
# Note the need use of -band 0xff to limit the result to 8 bits - see
# explanation below.
# -> 218 == 0xda = 11011010
$bitsInCommon = [byte] (-bnot ($firstNr -bxor $secondNr) -band 0xff)

# Convert the bit field back into a binary representation, 0-padded to 8 chars.
# -> '11011010'
$bitsInComminBin = [Convert]::ToString($bitsInCommon, 2).PadLeft(8, '0')

# Produce the desired output format with string manipulation
$result = ([char[]] $bitsInComminBin -replace '1', 'r' -replace '0', 'f') -join ' '

# Output the result
[ordered] @{
  'first Nr' = $firstNrBin
  'second Nr' = $secondNrBin
  'Result' = $result
}
上述收益率:

名称值
----                           -----
第一批10001
第二批1010100
结果r r f r f r f

请注意,PowerShell的位运算符将
[int]
-
System.Int32
输出为最小的数据类型,即带符号的数字

因此,您必须使用
-band
运算符显式屏蔽多余的位,因为直接回溯到无符号类型是不起作用的

在本案中:

$firstNr -bxor $secondNr # 141 -bxor 168
产生
37
作为
[int]
值,即以下32位:

00000000000000000000000000100101
-bnot
应用于此
[int]
将产生按位补码:

11111111111111111111111111011010
作为一个
[int]
,这是一个负数,因为设置了高位:
-38

您不能直接将其转换回无符号类型,如
[byte]
以仅获取最低的8位,但您可以使用
-band
和位掩码
0xff
将前8位之外的所有位归零,但请注意,此时的结果仍然是
[int]

-bnot -38 -band 0xff
产生以下位:

00000000000000000000000000100101
作为一个
[int]
,这是
37
,您可以安全地将其转换回
[byte]
一个带注释的解决方案:

$firstNrBin  = '1 0 0 0 1 1 0 1'
$secondNrBin = '1 0 1 0 1 0 0 0'

# Convert the binary number strings to [byte]s (unsigned 8-bit values).
$firstNr = [Convert]::ToByte($firstNrBin -replace ' ', 2)   # 141 == 0x8d
$secondNr = [Convert]::ToByte($secondNrBin -replace ' ', 2) # 168 == 0xa8

# Perform bitwise XOR logic and negate the result to get a bit field
# that reflects the bits where the input numbers match.
# Note the need use of -band 0xff to limit the result to 8 bits - see
# explanation below.
# -> 218 == 0xda = 11011010
$bitsInCommon = [byte] (-bnot ($firstNr -bxor $secondNr) -band 0xff)

# Convert the bit field back into a binary representation, 0-padded to 8 chars.
# -> '11011010'
$bitsInComminBin = [Convert]::ToString($bitsInCommon, 2).PadLeft(8, '0')

# Produce the desired output format with string manipulation
$result = ([char[]] $bitsInComminBin -replace '1', 'r' -replace '0', 'f') -join ' '

# Output the result
[ordered] @{
  'first Nr' = $firstNrBin
  'second Nr' = $secondNrBin
  'Result' = $result
}
上述收益率:

名称值
----                           -----
第一批10001
第二批1010100
结果r r f r f r f

请注意,PowerShell的位运算符将
[int]
-
System.Int32
输出为最小的数据类型,即带符号的数字

因此,您必须使用
-band
运算符显式屏蔽多余的位,因为直接回溯到无符号类型是不起作用的

在本案中:

$firstNr -bxor $secondNr # 141 -bxor 168
产生
37
作为
[int]
值,即以下32位:

00000000000000000000000000100101
-bnot
应用于此
[int]
将产生按位补码:

11111111111111111111111111011010
作为一个
[int]
,这是一个负数,因为设置了高位:
-38

您不能直接将其转换回无符号类型,如
[byte]
以仅获取最低的8位,但您可以使用
-band
和位掩码
0xff
将前8位之外的所有位归零,但请注意,此时的结果仍然是
[int]

-bnot -38 -band 0xff
产生以下位:

00000000000000000000000000100101

作为一个
[int]
,这是
37
,您可以安全地将其返回到
[byte]

$Result=-bNot$First-xor$Second
$Result=-bNot$First-xor$Second
非常感谢您回答我的问题。但是有没有可能做比特而不是数字呢。关于安全。。。在封面下,它们是一样的东西;i、 一个数字只是一个位的集合。所以只有在显示或输入的时候,这才有区别。。。我添加了两个函数,允许您将显示值从/转换为显示值(我使用了1和0,但您可以轻松地将其修改为使用
r
s和
f
s。非常好。感谢您的帮助。但请注意,
-bnot($a-bxor$b)
不会产生
11111 010
250
),它产生
11111111111111111111 010
-6
),因为PowerShell的二进制运算符返回的最小数据类型是
[int]
,所以需要屏蔽较高的位。不需要定制函数,因为.NET直接支持这些转换:
$bin=[Convert]::ToString(9,2)
[Convert]:ToByte($bin,2)
谢谢@mklement0,我不知道第二个(基本)这些函数的参数。很好的一个。非常感谢你回答我的问题。但是有没有可能做位而不是数字。再加上…在封面下,它们是一样的;也就是说,一个数字只是一个位的集合。所以只有在显示或输入的时候,这才有区别…我添加了两个函数ch允许您从显示值转换到显示值(我使用了1和0,但是您可以很容易地修改它以使用
r
s和
f
s.PERFECT。感谢一个loot.Help,但请注意
-bnot($a-bxor$b)
不会产生
11111 010
250
),它会产生
11111111111 010
-6
),因为PowerShell的二进制运算符返回的最小数据类型是
[int]
,所以您需要屏蔽较高的位。不需要自定义函数,因为.NET直接支持这些转换:
$bin=[Convert]::ToString(9,2)
[Convert]::ToByte($bin,2)
谢谢@mklement0,我不知道这些函数的第二个(基本)参数。很好。