Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

2's Compliment in Ruby

For general discussion related FlowStone

2's Compliment in Ruby

Postby aronb » Tue Nov 03, 2015 4:04 am

Hi,

How do you do 2's Compliment conversion in Ruby ?

I have a 16bit 2's compliment number that needs to be signed integer -32768 to 32767

thanks for any input...

Aron
User avatar
aronb
 
Posts: 154
Joined: Sun Apr 17, 2011 3:08 am
Location: Florida, USA

Re: 2's Compliment in Ruby

Postby KG_is_back » Tue Nov 03, 2015 11:39 am

Numbers in ruby are 2's compliment. If I assume correctly, your 16bit signed number is being displayed in unsigned format? If that is correct, simply subtract 2^16 in number is above or equal 2^15

Code: Select all
def unsigned_to_signed(x,bitdeapth)
if x>2**(bitdeapth-1)
x-2**bitdeapth
else
x
end
end

KG_is_back
 
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: 2's Compliment in Ruby

Postby aronb » Fri Nov 06, 2015 7:18 pm

Thank You KG_is_back ! ! !

I initially did the > test incorrectly... DOH

Works perfectly now :D

Have a great weekend,

Aron
User avatar
aronb
 
Posts: 154
Joined: Sun Apr 17, 2011 3:08 am
Location: Florida, USA


Return to General

Who is online

Users browsing this forum: No registered users and 47 guests

cron