Posted: 25 Feb 2016, 17:41
Hi !
building bitRead and bitWrite modules....
I have trouble with integers !
I'd like to access and set each bit of a 32 bit integer
if I set the bit #31, the output becomes negative
and everything become mixed up...
actually it's behaving weirdly as soon as I set the 25th bit
I could actually limit the module to 24 bits, but as I use OSC and OSC can send 32 bit integers, could be good to manage 32 bits with the bit Write and bitRead modules
in .h
in .cpp
building bitRead and bitWrite modules....
I have trouble with integers !
I'd like to access and set each bit of a 32 bit integer
if I set the bit #31, the output becomes negative
and everything become mixed up...
actually it's behaving weirdly as soon as I set the 25th bit
I could actually limit the module to 24 bits, but as I use OSC and OSC can send 32 bit integers, could be good to manage 32 bits with the bit Write and bitRead modules
in .h
Code: Select all
...
private:
//int bitRead(int value, int bit);
int bitWrite(unsigned long int value, int bit, int bitValue);
//-------------------------------------------------------------------------
// parameters events
UsineEventPtr input;
UsineEventPtr output;
UsineEventPtr bitValue;
UsineEventPtr bitOrder;
unsigned long int m_input = 0;
int m_bitOrder = 0;
int m_bitValue = 0;Code: Select all
...
void bitwiseMath::onCallBack(UsineMessage *Message)
{
if (Message->message == NOTIFY_MSG_USINE_CALLBACK && Message->lParam == MSG_CHANGE)
{
switch (Message->wParam)
{
case 0:
m_input = sdkGetEvtData(input);
sdkSetEvtData(output, bitWrite(m_input, m_bitOrder,m_bitValue));
break;
case 1:
m_bitValue = int(sdkGetEvtData(bitValue));
sdkSetEvtData(output, bitWrite(m_input, m_bitOrder, m_bitValue));
break;
case 2:
m_bitOrder = int(sdkGetEvtData(bitOrder));
sdkSetEvtData(output, bitWrite(m_input, m_bitOrder, m_bitValue));
break;
// default case
default:
break;
}
}
}
...
int bitwiseMath::bitWrite(unsigned long int value, int bit, int bitValue) {
if (bitValue == 1) {
value = value | (1 << bit);
}
else if (bitValue == 0) {
value = value & ~(1 << bit);
}
else {}
return value;
}