Hamming Distance comparison with powershell











up vote
0
down vote

favorite
1












How can i compare 8 bit with another 8 bit to have something like this:



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


Thanks










share|improve this question




















  • 2




    $Result = -bNot $First -xor $Second
    – iRon
    Nov 19 at 22:09















up vote
0
down vote

favorite
1












How can i compare 8 bit with another 8 bit to have something like this:



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


Thanks










share|improve this question




















  • 2




    $Result = -bNot $First -xor $Second
    – iRon
    Nov 19 at 22:09













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





How can i compare 8 bit with another 8 bit to have something like this:



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


Thanks










share|improve this question















How can i compare 8 bit with another 8 bit to have something like this:



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


Thanks







powershell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 22:02









Drew

1,148416




1,148416










asked Nov 19 at 21:50









Khabat Rammo

113




113








  • 2




    $Result = -bNot $First -xor $Second
    – iRon
    Nov 19 at 22:09














  • 2




    $Result = -bNot $First -xor $Second
    – iRon
    Nov 19 at 22:09








2




2




$Result = -bNot $First -xor $Second
– iRon
Nov 19 at 22:09




$Result = -bNot $First -xor $Second
– iRon
Nov 19 at 22:09












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










An annotated solution:



$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
}


The above yields:



Name                           Value
---- -----
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




Note that PowerShell's bitwise operators output [int] - System.Int32 as the smallest data type, i.e., signed numbers.



Therefore you have to mask out extra bits explicitly with the -band operator, because directly casting back to an unsigned type doesn't work.



In the case at hand:



$firstNr -bxor $secondNr # 141 -bxor 168


produces 37 as an [int] value, i.e, the following 32 bits:



00000000000000000000000000100101


Applying -bnot to this [int] yields the bitwise complement:



11111111111111111111111111011010


As an [int], this is a negative number, because the high bit is set: -38



You cannot cast this directly back to an unsigned type such as [byte] to only get the lowest 8 bits, but you can use -band with bit mask 0xff to zero out all bits beyond the first 8 ones - but note that the result is still an [int] at that point:



-bnot -38 -band 0xff


yields the following bits:



00000000000000000000000000100101


As an [int], this is 37, which you can safely cast back to [byte].






share|improve this answer






























    up vote
    2
    down vote













    You can use bitwise comparison operators:



    e.g.



    [byte]$a = 9       #00001001
    [byte]$b = 12 #00001100

    -bnot ($a -bxor $b) #11111010

    $a -band $b #00001000
    $a -bor $b #00001101


    For your scenario you're looking at -bxor functionality, only taking the negative (-bnot) of the result.





    If you need to see the flags themselves, you can use methods such as these:



    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'





    share|improve this answer



















    • 1




      Thanks a lot for answering my question. But is there any possibility to do the bits and not numbers. Regards
      – Khabat Rammo
      Nov 19 at 22:27










    • Sure... Under the covers they're the same thing; i.e. a number's just a collection of bits. So it's only at display or input time that this makes a difference... I've added a couple of functions which allow you to convert from/to the display values (I've used 1s and 0s, but you could easily amend it to work with rs and fs.
      – JohnLBevan
      Nov 19 at 22:38






    • 1




      PERFECT. Thanks a loot.
      – Khabat Rammo
      Nov 19 at 22:43






    • 1




      Helpful, but note that -bnot ($a -bxor $b) doesn't yield 11111010 (250), it yields 11111111111111111111111111111010 (-6), because the smallest data type that PowerShell's binary operators return is [int], so you need to mask out the higher bits. No real need for custom functions, because .NET supports these conversions directly: $bin = [Convert]::ToString(9, 2) and [Convert]::ToByte($bin, 2)
      – mklement0
      Nov 20 at 4:48






    • 1




      Thanks @mklement0, I wasn't aware of the second (base) parameter for those functions. Nice one.
      – JohnLBevan
      Nov 20 at 7:09











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383139%2fhamming-distance-comparison-with-powershell%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    An annotated solution:



    $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
    }


    The above yields:



    Name                           Value
    ---- -----
    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




    Note that PowerShell's bitwise operators output [int] - System.Int32 as the smallest data type, i.e., signed numbers.



    Therefore you have to mask out extra bits explicitly with the -band operator, because directly casting back to an unsigned type doesn't work.



    In the case at hand:



    $firstNr -bxor $secondNr # 141 -bxor 168


    produces 37 as an [int] value, i.e, the following 32 bits:



    00000000000000000000000000100101


    Applying -bnot to this [int] yields the bitwise complement:



    11111111111111111111111111011010


    As an [int], this is a negative number, because the high bit is set: -38



    You cannot cast this directly back to an unsigned type such as [byte] to only get the lowest 8 bits, but you can use -band with bit mask 0xff to zero out all bits beyond the first 8 ones - but note that the result is still an [int] at that point:



    -bnot -38 -band 0xff


    yields the following bits:



    00000000000000000000000000100101


    As an [int], this is 37, which you can safely cast back to [byte].






    share|improve this answer



























      up vote
      1
      down vote



      accepted










      An annotated solution:



      $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
      }


      The above yields:



      Name                           Value
      ---- -----
      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




      Note that PowerShell's bitwise operators output [int] - System.Int32 as the smallest data type, i.e., signed numbers.



      Therefore you have to mask out extra bits explicitly with the -band operator, because directly casting back to an unsigned type doesn't work.



      In the case at hand:



      $firstNr -bxor $secondNr # 141 -bxor 168


      produces 37 as an [int] value, i.e, the following 32 bits:



      00000000000000000000000000100101


      Applying -bnot to this [int] yields the bitwise complement:



      11111111111111111111111111011010


      As an [int], this is a negative number, because the high bit is set: -38



      You cannot cast this directly back to an unsigned type such as [byte] to only get the lowest 8 bits, but you can use -band with bit mask 0xff to zero out all bits beyond the first 8 ones - but note that the result is still an [int] at that point:



      -bnot -38 -band 0xff


      yields the following bits:



      00000000000000000000000000100101


      As an [int], this is 37, which you can safely cast back to [byte].






      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        An annotated solution:



        $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
        }


        The above yields:



        Name                           Value
        ---- -----
        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




        Note that PowerShell's bitwise operators output [int] - System.Int32 as the smallest data type, i.e., signed numbers.



        Therefore you have to mask out extra bits explicitly with the -band operator, because directly casting back to an unsigned type doesn't work.



        In the case at hand:



        $firstNr -bxor $secondNr # 141 -bxor 168


        produces 37 as an [int] value, i.e, the following 32 bits:



        00000000000000000000000000100101


        Applying -bnot to this [int] yields the bitwise complement:



        11111111111111111111111111011010


        As an [int], this is a negative number, because the high bit is set: -38



        You cannot cast this directly back to an unsigned type such as [byte] to only get the lowest 8 bits, but you can use -band with bit mask 0xff to zero out all bits beyond the first 8 ones - but note that the result is still an [int] at that point:



        -bnot -38 -band 0xff


        yields the following bits:



        00000000000000000000000000100101


        As an [int], this is 37, which you can safely cast back to [byte].






        share|improve this answer














        An annotated solution:



        $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
        }


        The above yields:



        Name                           Value
        ---- -----
        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




        Note that PowerShell's bitwise operators output [int] - System.Int32 as the smallest data type, i.e., signed numbers.



        Therefore you have to mask out extra bits explicitly with the -band operator, because directly casting back to an unsigned type doesn't work.



        In the case at hand:



        $firstNr -bxor $secondNr # 141 -bxor 168


        produces 37 as an [int] value, i.e, the following 32 bits:



        00000000000000000000000000100101


        Applying -bnot to this [int] yields the bitwise complement:



        11111111111111111111111111011010


        As an [int], this is a negative number, because the high bit is set: -38



        You cannot cast this directly back to an unsigned type such as [byte] to only get the lowest 8 bits, but you can use -band with bit mask 0xff to zero out all bits beyond the first 8 ones - but note that the result is still an [int] at that point:



        -bnot -38 -band 0xff


        yields the following bits:



        00000000000000000000000000100101


        As an [int], this is 37, which you can safely cast back to [byte].







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 at 4:56

























        answered Nov 20 at 4:34









        mklement0

        123k20234265




        123k20234265
























            up vote
            2
            down vote













            You can use bitwise comparison operators:



            e.g.



            [byte]$a = 9       #00001001
            [byte]$b = 12 #00001100

            -bnot ($a -bxor $b) #11111010

            $a -band $b #00001000
            $a -bor $b #00001101


            For your scenario you're looking at -bxor functionality, only taking the negative (-bnot) of the result.





            If you need to see the flags themselves, you can use methods such as these:



            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'





            share|improve this answer



















            • 1




              Thanks a lot for answering my question. But is there any possibility to do the bits and not numbers. Regards
              – Khabat Rammo
              Nov 19 at 22:27










            • Sure... Under the covers they're the same thing; i.e. a number's just a collection of bits. So it's only at display or input time that this makes a difference... I've added a couple of functions which allow you to convert from/to the display values (I've used 1s and 0s, but you could easily amend it to work with rs and fs.
              – JohnLBevan
              Nov 19 at 22:38






            • 1




              PERFECT. Thanks a loot.
              – Khabat Rammo
              Nov 19 at 22:43






            • 1




              Helpful, but note that -bnot ($a -bxor $b) doesn't yield 11111010 (250), it yields 11111111111111111111111111111010 (-6), because the smallest data type that PowerShell's binary operators return is [int], so you need to mask out the higher bits. No real need for custom functions, because .NET supports these conversions directly: $bin = [Convert]::ToString(9, 2) and [Convert]::ToByte($bin, 2)
              – mklement0
              Nov 20 at 4:48






            • 1




              Thanks @mklement0, I wasn't aware of the second (base) parameter for those functions. Nice one.
              – JohnLBevan
              Nov 20 at 7:09















            up vote
            2
            down vote













            You can use bitwise comparison operators:



            e.g.



            [byte]$a = 9       #00001001
            [byte]$b = 12 #00001100

            -bnot ($a -bxor $b) #11111010

            $a -band $b #00001000
            $a -bor $b #00001101


            For your scenario you're looking at -bxor functionality, only taking the negative (-bnot) of the result.





            If you need to see the flags themselves, you can use methods such as these:



            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'





            share|improve this answer



















            • 1




              Thanks a lot for answering my question. But is there any possibility to do the bits and not numbers. Regards
              – Khabat Rammo
              Nov 19 at 22:27










            • Sure... Under the covers they're the same thing; i.e. a number's just a collection of bits. So it's only at display or input time that this makes a difference... I've added a couple of functions which allow you to convert from/to the display values (I've used 1s and 0s, but you could easily amend it to work with rs and fs.
              – JohnLBevan
              Nov 19 at 22:38






            • 1




              PERFECT. Thanks a loot.
              – Khabat Rammo
              Nov 19 at 22:43






            • 1




              Helpful, but note that -bnot ($a -bxor $b) doesn't yield 11111010 (250), it yields 11111111111111111111111111111010 (-6), because the smallest data type that PowerShell's binary operators return is [int], so you need to mask out the higher bits. No real need for custom functions, because .NET supports these conversions directly: $bin = [Convert]::ToString(9, 2) and [Convert]::ToByte($bin, 2)
              – mklement0
              Nov 20 at 4:48






            • 1




              Thanks @mklement0, I wasn't aware of the second (base) parameter for those functions. Nice one.
              – JohnLBevan
              Nov 20 at 7:09













            up vote
            2
            down vote










            up vote
            2
            down vote









            You can use bitwise comparison operators:



            e.g.



            [byte]$a = 9       #00001001
            [byte]$b = 12 #00001100

            -bnot ($a -bxor $b) #11111010

            $a -band $b #00001000
            $a -bor $b #00001101


            For your scenario you're looking at -bxor functionality, only taking the negative (-bnot) of the result.





            If you need to see the flags themselves, you can use methods such as these:



            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'





            share|improve this answer














            You can use bitwise comparison operators:



            e.g.



            [byte]$a = 9       #00001001
            [byte]$b = 12 #00001100

            -bnot ($a -bxor $b) #11111010

            $a -band $b #00001000
            $a -bor $b #00001101


            For your scenario you're looking at -bxor functionality, only taking the negative (-bnot) of the result.





            If you need to see the flags themselves, you can use methods such as these:



            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'






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 19 at 22:43

























            answered Nov 19 at 22:09









            JohnLBevan

            14.1k145102




            14.1k145102








            • 1




              Thanks a lot for answering my question. But is there any possibility to do the bits and not numbers. Regards
              – Khabat Rammo
              Nov 19 at 22:27










            • Sure... Under the covers they're the same thing; i.e. a number's just a collection of bits. So it's only at display or input time that this makes a difference... I've added a couple of functions which allow you to convert from/to the display values (I've used 1s and 0s, but you could easily amend it to work with rs and fs.
              – JohnLBevan
              Nov 19 at 22:38






            • 1




              PERFECT. Thanks a loot.
              – Khabat Rammo
              Nov 19 at 22:43






            • 1




              Helpful, but note that -bnot ($a -bxor $b) doesn't yield 11111010 (250), it yields 11111111111111111111111111111010 (-6), because the smallest data type that PowerShell's binary operators return is [int], so you need to mask out the higher bits. No real need for custom functions, because .NET supports these conversions directly: $bin = [Convert]::ToString(9, 2) and [Convert]::ToByte($bin, 2)
              – mklement0
              Nov 20 at 4:48






            • 1




              Thanks @mklement0, I wasn't aware of the second (base) parameter for those functions. Nice one.
              – JohnLBevan
              Nov 20 at 7:09














            • 1




              Thanks a lot for answering my question. But is there any possibility to do the bits and not numbers. Regards
              – Khabat Rammo
              Nov 19 at 22:27










            • Sure... Under the covers they're the same thing; i.e. a number's just a collection of bits. So it's only at display or input time that this makes a difference... I've added a couple of functions which allow you to convert from/to the display values (I've used 1s and 0s, but you could easily amend it to work with rs and fs.
              – JohnLBevan
              Nov 19 at 22:38






            • 1




              PERFECT. Thanks a loot.
              – Khabat Rammo
              Nov 19 at 22:43






            • 1




              Helpful, but note that -bnot ($a -bxor $b) doesn't yield 11111010 (250), it yields 11111111111111111111111111111010 (-6), because the smallest data type that PowerShell's binary operators return is [int], so you need to mask out the higher bits. No real need for custom functions, because .NET supports these conversions directly: $bin = [Convert]::ToString(9, 2) and [Convert]::ToByte($bin, 2)
              – mklement0
              Nov 20 at 4:48






            • 1




              Thanks @mklement0, I wasn't aware of the second (base) parameter for those functions. Nice one.
              – JohnLBevan
              Nov 20 at 7:09








            1




            1




            Thanks a lot for answering my question. But is there any possibility to do the bits and not numbers. Regards
            – Khabat Rammo
            Nov 19 at 22:27




            Thanks a lot for answering my question. But is there any possibility to do the bits and not numbers. Regards
            – Khabat Rammo
            Nov 19 at 22:27












            Sure... Under the covers they're the same thing; i.e. a number's just a collection of bits. So it's only at display or input time that this makes a difference... I've added a couple of functions which allow you to convert from/to the display values (I've used 1s and 0s, but you could easily amend it to work with rs and fs.
            – JohnLBevan
            Nov 19 at 22:38




            Sure... Under the covers they're the same thing; i.e. a number's just a collection of bits. So it's only at display or input time that this makes a difference... I've added a couple of functions which allow you to convert from/to the display values (I've used 1s and 0s, but you could easily amend it to work with rs and fs.
            – JohnLBevan
            Nov 19 at 22:38




            1




            1




            PERFECT. Thanks a loot.
            – Khabat Rammo
            Nov 19 at 22:43




            PERFECT. Thanks a loot.
            – Khabat Rammo
            Nov 19 at 22:43




            1




            1




            Helpful, but note that -bnot ($a -bxor $b) doesn't yield 11111010 (250), it yields 11111111111111111111111111111010 (-6), because the smallest data type that PowerShell's binary operators return is [int], so you need to mask out the higher bits. No real need for custom functions, because .NET supports these conversions directly: $bin = [Convert]::ToString(9, 2) and [Convert]::ToByte($bin, 2)
            – mklement0
            Nov 20 at 4:48




            Helpful, but note that -bnot ($a -bxor $b) doesn't yield 11111010 (250), it yields 11111111111111111111111111111010 (-6), because the smallest data type that PowerShell's binary operators return is [int], so you need to mask out the higher bits. No real need for custom functions, because .NET supports these conversions directly: $bin = [Convert]::ToString(9, 2) and [Convert]::ToByte($bin, 2)
            – mklement0
            Nov 20 at 4:48




            1




            1




            Thanks @mklement0, I wasn't aware of the second (base) parameter for those functions. Nice one.
            – JohnLBevan
            Nov 20 at 7:09




            Thanks @mklement0, I wasn't aware of the second (base) parameter for those functions. Nice one.
            – JohnLBevan
            Nov 20 at 7:09


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383139%2fhamming-distance-comparison-with-powershell%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Tonle Sap (See)

            I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

            Guatemaltekische Davis-Cup-Mannschaft