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
3 posts
• Page 1 of 1
2's Compliment in Ruby
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
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
-
aronb - Posts: 154
- Joined: Sun Apr 17, 2011 3:08 am
- Location: Florida, USA
Re: 2's Compliment in Ruby
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
Thank You KG_is_back ! ! !
I initially did the > test incorrectly... DOH
Works perfectly now
Have a great weekend,
Aron
I initially did the > test incorrectly... DOH
Works perfectly now
Have a great weekend,
Aron
-
aronb - Posts: 154
- Joined: Sun Apr 17, 2011 3:08 am
- Location: Florida, USA
3 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 58 guests