We’ve looked at the binary functions – now its time to look at the hex functions starting with addition.

 001 002 003 004 005 006 007 008 009 010 011 012 013 014 function Get-HexSum { param(     [string]\$inputvalue1,     [string]\$inputvalue2 ) ## check valid hex numbers     Test-Hex \$inputvalue1     Test-Hex \$inputvalue2     \$sum = (ConvertTo-Decimal -inputvalue \$inputvalue1 -hex) `     + (ConvertTo-Decimal -inputvalue \$inputvalue2 -hex)     ConvertTo-Hex -inputvalue \$sum }

This is a direct copy of the binary addition function – just changed to accept hex input instead.

Again we test the hex number is actually hex – convert to decimal, add, and convert back to hex

Establishing a pattern helps make things easier for producing related functions.

## Bitwise Operations

Continuing our look at binary operations I decided I wanted to be able to perform the bitwise operations – band, bor, bxor and get the results back as binary.  We already have operators for these actions in PowerShell so its just a matter of wrapping the appropriate conversion code around them.  I’ll show the code for band here.  The other two are very similar and will be available in the module when I post it on codeplex.

 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 function Get-BinaryAND { param(     [string]\$inputvalue1,     [string]\$inputvalue2,     [switch]\$showsum )     if (\$inputvalue1.length -ne \$inputvalue2.length){Throw "Input values must be same length"} ## check valid binary numbers ## move this to a validation test     Test-Binary \$inputvalue1     Test-Binary \$inputvalue2     \$res = (ConvertTo-Decimal -inputvalue \$inputvalue1 -binary) `          -band (ConvertTo-Decimal -inputvalue \$inputvalue2 -binary)     \$ans = (ConvertTo-Binary -inputvalue \$res).PadLeft(\$inputvalue1.length, "0")     if (\$showsum) {         \$inputvalue1         \$inputvalue2         "-" * \$inputvalue1.length         \$ans     }     else {         \$ans     } }

Two binary numbers as input as we saw in the addition\subtraction functions.  I have changed the names to be consistent across the module. Perform the standard checks and then convert them to decimal and perform the band. The result is converted back to binary – padding left with zeros if required.

I decided to show the inputs and output if required – may be a good learning tool to understand this.

usage is straight forward

PS> Get-BinaryAND -inputvalue1 1010 -inputvalue2 0011
0010
PS> Get-BinaryAND -inputvalue1 10111010 -inputvalue2 01110011 -showsum
10111010
01110011
--------
00110010

The module contents stand at

ConvertTo-Binary
ConvertTo-Decimal
ConvertTo-Hex
Get-BinaryAND
Get-BinaryDifference
Get-BinaryOR
Get-BinarySum
Get-BinaryXOR
Test-Binary
Test-Hex

I need to add a couple of functions to do hex addition and subtraction and then its done

We recently looked at converting numbers to binary and hex.  I then thought that I sometimes need to perform simple arithmetic in these bases so – binary addition.

We can use the functions we already have.  Convert a couple of numbers to binary

PS> \$a = Convert-ToBinary -inputvalue 51
PS> \$b = Convert-ToBinary -inputvalue 39
PS> \$a
110011
PS> \$b
100111

sum them by converting the binary back to decimal, performing the sum and converting back to binary.

PS> \$c = Convert-ToBinary -inputvalue ((Convert-ToDecimal -inputvalue \$a -binary) + (Convert-ToDecimal -inputvalue \$b -binary))
PS> \$c
1011010

A quick check on the result

PS> Convert-ToDecimal -inputvalue \$c -binary
90

That works OK  but its a bit messy – too much typing. So I wrote Get-BinarySum

 001 002 003 004 005 006 007 008 009 010 011 012 013 014 function Get-BinarySum { param(     [string]\$value1,     [string]\$value2 ) ## check valid binary numbers ## move this to a validation test Test-Binary \$value1 Test-Binary \$value2 \$sum = (Convert-ToDecimal -inputvalue \$value1 -binary) + (Convert-ToDecimal -inputvalue \$value2 -binary) Convert-ToBinary -inputvalue \$sum }

Get the two binary numbers – test that they are binary.  Test-Binary is a helper function created from the code used in Convert-ToDecimal

The sum is performed by converting each binary to a decimal, summing them and converting the result back to a binary.

Bingo.  This forms the framework for other functions.

I have created the first download for the PowerShell Admin Modules. This will be a set of of modules including code previously published on my blogs at:

http://msmvps.com/blogs/RichardSiddaway/Default.aspx

http://itknowledgeexchange.techtarget.com/powershell/

http://richardsiddaway.spaces.live.com/

The first release comprises a module for working with shares and can be found at:

http://psam.codeplex.com/

It has the following functions:

• Get-Share