Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Other Applications of Bitwise Operations

0.00/5 (No votes)
23 Sep 2012 1  
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)
   List1.AddItem mArray(index)
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)
   List1.AddItem mArray(index)
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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here