Created
March 3, 2011 06:31
-
-
Save jamesdaniels/852432 to your computer and use it in GitHub Desktop.
Oldest ruby file (of my own creation) I could locate on my hard-drive, dated 2/7/04 - man I was bad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#************************************** this will flip all the bits passed to it * * * * * * * | |
def flip(binary) | |
# replace 1 with 2, then 0 with 1, and then 2 with 0 | |
return ((binary.tr("1","2")).tr("0","1")).tr("2","0")\ | |
end | |
#************************************** adding implementation into the Integer class * * * * * | |
class Integer | |
def in_unsigned_binary(bits) #******* returns the unsigned binary form of the integer * * * | |
@binary = "" # hold the binary string | |
@result = self # hold the remaining value to be converted | |
@place = bits-1 # hold our current exponent | |
# make sure we are within the acceptable range | |
if self.abs >= 2**(bits) or self < 0 | |
return "n/a" | |
else | |
# go forth and binaryize | |
bits.times do | |
if @result >= 2**@place # if you are bigger than the current place | |
@binary += "1" # add a 1 to the binary | |
@result -= 2**@place # and subtract the place value | |
else # else | |
@binary += "0" # add 0 to our binary | |
end | |
@place -= 1 # drop our exponent | |
end | |
return @binary # return our results | |
end | |
end | |
def in_signed_magnitude(bits) #****** returns the signed magnitude form of the integer * * * | |
@sign = "" # hold the sign bit | |
@magnitude = (self.abs).in_unsigned_binary(bits-1) # holds our magnitude | |
# check to see if it breaks the bounds | |
if @magnitude == "n/a" | |
return "n/a" | |
else | |
# figure the sign | |
if self < 0 | |
@sign = "1" | |
else | |
@sign = "0" | |
end | |
return @sign + @magnitude # return it | |
end | |
end | |
def in_bias_127(bits) #************** returns the bias-127 form of the integer * * * * * * * | |
return (self+127).in_unsigned_binary(bits) | |
end | |
def in_ones_complement(bits) #******* returns the ones-complement form of the integer * * * | |
@binary = (self.abs).in_unsigned_binary(bits) | |
if self.abs >= 2**(bits-1) | |
return "n/a" | |
else if self > 0 | |
return @binary # if it's greater than 0, just return the unsigned | |
else | |
return flip(@binary) # else flip and return | |
end end | |
end | |
def in_twos_complement(bits) #******* returns the twos-complement form of the integer * * * | |
@ones = (self.abs).in_unsigned_binary(bits) # hold the ones complement, for now hold unsigned | |
@twos = "0"*bits # and the twos, defaults to 0xbits | |
if @ones == "n/a" or self == 2**(bits-1) | |
return "n/a" | |
else if self < 0 | |
@ones = flip(@ones) | |
@twos = @ones.slice([email protected]("0")) # if it is negative simulate the addition of one | |
@twos += flip(@ones.slice(@ones.rindex("0")[email protected])) # to the ones complement | |
return @twos | |
else if self == 0 | |
return @twos # if it's zero, return zero | |
else | |
return @ones # if it's positive, return unsigned, remember ones is still unsigned at this point | |
end end end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment