Click here to Skip to main content
15,891,372 members
Articles / Programming Languages / Visual Basic 6
Tip/Trick

Other Applications of Bitwise Operations

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
23 Sep 2012CPOL 12.4K   2  
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:

VB.NET
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.

VB.NET
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.

Image 1

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

Image 2

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

Image 3

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

VB.NET
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
VB.NET
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Philippines Philippines
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --