12,510,189 members (51,370 online)
Tip/Trick
alternative version

6.6K views
2 bookmarked
Posted

Other Applications of Bitwise Operations

, 23 Sep 2012 CPOL
 Rate this:
Harmonizing music scales

Introduction

This tip presents two simple applications of bit-wise operators: the "`Imp`" operator and the "`And`" operator.

The first function uses the `Imp `operator to find chord types whose notes are a subset of a given musical scale:

```Function ReturnChordNumbers(ParentScaleNumber As Integer) As Integer()
Dim n As Integer
Dim mChordNumbers() As Integer, index As Integer

For n = 2048 To 4095

If (n Imp ParentScaleNumber) = -1 Then
index = index + 1
ReDim Preserve mChordNumbers(1 To index)
mChordNumbers(index) = n
End If

Next n

ReturnChordNumbers = mChordNumbers
Erase mChordNumbers
End Function    ```

The second function does the reverse process and returns the scale types (or "larger" chord types) that contain all the notes of a specified chord. It uses the `And `operator.

```Function ReturnScaleNumbers(DecimalChordNumber As Integer) As Integer()
Dim n As Integer
Dim mScaleNumbers() As Integer, index As Integer

For n = 2048 To 4095
If (n And DecimalChordNumber) = DecimalChordNumber Then
index = index + 1
ReDim Preserve mScaleNumbers(1 To index)
mScaleNumbers(index) = n
End If
Next n
ReturnScaleNumbers = mScaleNumbers
End Function   ```

In the functions written above, integer variable `n `has a range of `2048 `to `4095`, which represents all combinations of root formulas that can be extracted from the 12 intervals of the chromatic scale formula.

Here are some examples of common music scales that could be harmonized using the first function,

while here are some widely used chord types that could be entered into the second function:

Both functions would require a decimal value as the input and can be called by adding supporting procedures such as those written below:

```Private Sub Command1_Click()
Dim mArray() As Integer
Dim index As Integer

mArray = ReturnChordNumbers(txtInput.Text)

List1.Clear
For index = LBound(mArray) To UBound(mArray)
Next index
End Sub```
```Private Sub Command2_Click()
Dim mArray() As Integer

Dim index As Integer

mArray = ReturnScaleNumbers(txtInput.Text)
List1.Clear
For index = LBound(mArray) To UBound(mArray)
Next index
End Sub

' // Another alternative to the 1st function.
' // If we were to use the "Or" operator, the same output would result.

Function ReturnChordNumbers_1B(ParentScaleNumber As Integer) As Integer()
Dim n As Integer
Dim mChordNumbers() As Integer, index As Integer

For n = 2048 To 4095

If (ParentScaleNumber Or n) = ParentScaleNumber Then
'// also: (n Or ParentScaleNumber) = ParentScaleNumber
index = index + 1
ReDim Preserve mChordNumbers(1 To index)
mChordNumbers(index) = n
End If

Next n

ReturnChordNumbers_1B = mChordNumbers
Erase mChordNumbers
End Function```

Share

 Philippines
No Biography provided

You may also be interested in...

 Pro Pro