Welcome to %s forums

BrainModular Users Forum

Login Register

32 bit integer

Create your own modules in C++
Post Reply
User avatar
oli_lab
Member
Posts: 1261
Location: Brittany, France
Contact:

Unread post by oli_lab » 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

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;
in .cpp

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 | &#40;1 << bit&#41;;
	&#125;
	else if &#40;bitValue == 0&#41; &#123;
		value = value & ~&#40;1 << bit&#41;;
	&#125;
	else &#123;&#125;
	
	return value;
&#125;
http://oli-lab.org

Win11 Ryzen9/32GB RAM - RME MADIFACE - SSL alpha link 4-16 - OSC capable interfaces

follow OLI_LAB adventures on Mastodon
@olivar_premier@mastodon.social

Trogluddite
Member
Posts: 16
Contact:

Unread post by Trogluddite » 26 Feb 2016, 02:19

A change when you alter bit 25 would indicates that somewhere a 32 bit float is masquerading as true integer (it's a float that just happens to have an integer value).

In a float, bits 24-30 set the exponent value - which multiplies a number by successive powers of two. Bit 31 is always the sign bit, as you have seen.

Not quite sure yet from your code where this is happening, but the SetEvent and GetEvent API calls would be my guess, as those both take float values IIRC - even if the module is set to integer rounding.

Hope you get it fixed.

User avatar
oli_lab
Member
Posts: 1261
Location: Brittany, France
Contact:

Unread post by oli_lab » 26 Feb 2016, 09:31

HI,
I get to the same conclusion as you : everything getting out of the module is float, no int

I made bitRead an bitWrite up to 16 bits, not bad after all.

thanx
http://oli-lab.org

Win11 Ryzen9/32GB RAM - RME MADIFACE - SSL alpha link 4-16 - OSC capable interfaces

follow OLI_LAB adventures on Mastodon
@olivar_premier@mastodon.social

sm_jamieson
Member
Posts: 551
Contact:

Unread post by sm_jamieson » 26 Feb 2016, 12:09

All parameters including arrays seem to be floats in Usine, with the exception of colors, which are 32 bit unsigned values. See in the SDK

//-----------------------------------------------------------------------------
/// Usine native color format.
typedef ULong TColorUsine;

//-----------------------------------------------------------------------------
/// Type used in all parameters data.
typedef float TPrecision;

I have tried to send colors in an array and high bits get lost due to the floating point precision limits. But Color parameters obviously work.
So if you really need to sent 32 bit values, you could hijack a color parameter.

There was previously talk of using doubles (64 bit float) in Usine, but that was rejected as it would increase processing and memory overhead.

Simon.

User avatar
oli_lab
Member
Posts: 1261
Location: Brittany, France
Contact:

Unread post by oli_lab » 26 Feb 2016, 14:14

Thank you I understand,
anyway what is going out of osc receive message will be a float as well, it is no point of hacking the color parameter for a 32 bit bitread module.

it will be 24 bit max.
http://oli-lab.org

Win11 Ryzen9/32GB RAM - RME MADIFACE - SSL alpha link 4-16 - OSC capable interfaces

follow OLI_LAB adventures on Mastodon
@olivar_premier@mastodon.social

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests