Checking to see whether a bit shift will cause overflow

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,
I am messing around with bit manipulation, and thought Id make an arpeggiator go up and down a chord.
below is my routines. arpUp works perfectly. However it has the added advantage of when it:

if (maskUp < 0x10)

It has the advantage of mask being able to be less than 0x10.
arpDown needs to check whether mask > 0x8000 but it cant because thats the maximum size the variable can be. Ive kind of gotten around it by making a special case, but I really dont like that solution, because I have to hardcode in a value.
The other issue is that the number arpPosition starts on is shardcoded in.

Can anyone suggest some improvements to arpDown for me please.
Thanks

void Arpeggiator::arpUp(unsigned int chord) {
	arpPosition++;
	if (maskUp & chord) {
		arpeggioPlay(arpPosition);
	}
	maskUp >>= 1;
	if (maskUp < 0x10) {
		maskUp = 0x8000;
		arpPosition = startNote;
	}
}

void Arpeggiator::arpDown(unsigned int chord) {
	arpPosition--;
	if (arpPosition < startNote){
		arpPosition = startNote + 12;
	}
	if (maskDown & chord) {
		arpeggioPlay(arpPosition);
	}
	maskDown <<= 1;
	if (maskDown == 0x8000) {
		if (maskDown & chord) {
			arpeggioPlay(60);
		}
		maskDown = 0x10;
	}
}

mask and arpPosition are member variables so they hold their last value because it goes back to main after each completion and then gets called again on a timer tick - to keep the timing of my arpeggiator so i cant just do it in a while loop because I have other things to do, but it causes the problem of the resetting of the variables.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Check the bit before you shift.

Regards,
Steve A.

The Board helps those that help themselves.