Note that in the context of computer hardware a bit has one of two states, which we might call 0 and 1. In electronic hardware, voltage at two points are compared, and when the voltage at one point exceeds that of the other by a certain amount, it's the "1" state, and when the voltage is less than a certain amount it's the "0" state. You can get some noise immunity by changing the threshold after it has been crossed (moving it away from the current value), so it's probably not a good idea to assume that these two thresholds are equal.
It's possible to introduce other thresholds, to capture three states, but that creates additional complexities and issues (See http://en.trinary.ru/projects/setunws/ for an example of how a system built on this concept might look). Anyways, this system allows lets us represent small integers by treating them as unsigned binary numbers. But as we have noticed, dealing with signed integers raises additional issues. One approach is to treat one bit as a "sign" and to make that independent of the rest of the number. In eight bits, this might look like: 0 0 0 0 0 0 1 0 : 2 1 0 0 0 0 0 1 0 : -2 This is often called the "sign and magnitude" representation. But if we have built hardware for adding unsigned integers, it will not work properly on signed "sign and magnitude" numbers (it would produce the answer -4 for 2 + -2). If we use "twos complement" to represent numbers, we can use the same hardware that we use to add unsigned integers to add signed integers. To represent signed numbers using "twos complement" using n bits, we take negative numbers modulo 2^n. Meanwhile, monadic #: is supposed to give us the bit representation of a number. And it works fine for unsigned numbers, but when negative numbers are present in its argument it should perhaps include an extra bit of magnitude in its result. -- Raul On Sat, Dec 17, 2011 at 11:41 AM, Kip Murray <k...@math.uh.edu> wrote: > Linda, try this > > NB. 16-"bit" signed binary numbers > > NB. negative is - absolute value > > sbinary =: _1:`1:@.(0 <: ])"0 * (16#2) #: | > > value =: (2 p.~ |.)"1 > > NB. 2 7 1 8 p. y is +/ 2 7 1 8 * y ^ i. 4 > > sbadd =: ([: sbinary +&value)"1 > > sbmul =: ([: sbinary *&value)"1 > > sbneg =: ([: sbinary [: - value)"1 > > ] 'a b' =: sbinary _5 3 > 0 0 0 0 0 0 0 0 0 0 0 0 0 _1 0 _1 > 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 > > value a ,: b > _5 3 > > a (sbadd ,: sbmul) b > 0 0 0 0 0 0 0 0 0 0 0 0 0 0 _1 0 > 0 0 0 0 0 0 0 0 0 0 0 0 _1 _1 _1 _1 > > value a (sbadd ,: sbmul) b > _2 _15 > > sbneg a ,: b > 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 > 0 0 0 0 0 0 0 0 0 0 0 0 0 0 _1 _1 > > value sbneg a ,: b > 5 _3 > > ]f=: (?6 7$3){_1 0 1 > 0 _1 _1 0 1 _1 0 > _1 0 _1 1 0 1 1 > 0 _1 0 0 1 _1 0 > _1 1 1 0 _1 _1 1 > 1 _1 0 1 1 _1 0 > 0 _1 _1 _1 _1 1 _1 > > value f > _46 _69 _30 _21 42 _59 > > #. f > _46 _69 _30 _21 42 _59 > > sbinary value f > 0 0 0 0 0 0 0 0 0 0 _1 0 _1 _1 _1 0 > 0 0 0 0 0 0 0 0 0 _1 0 0 0 _1 0 _1 > 0 0 0 0 0 0 0 0 0 0 0 _1 _1 _1 _1 0 > 0 0 0 0 0 0 0 0 0 0 0 _1 0 _1 0 _1 > 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 > 0 0 0 0 0 0 0 0 0 0 _1 _1 _1 0 _1 _1 > > value sbinary value f > _46 _69 _30 _21 42 _59 > > Kip > > On 12/16/2011 9:16 PM, Linda Alvord wrote: > ... >> My contention is that if we, the users, could get used to negative numbers, >> as we have learned to like _123.132j432.23 we would embrace these six >> binary representations: >> >> f=:0,.0,.(?6 7$3){_1 0 1 >> f >> 0 0 _1 1 1 0 _1 _1 1 >> 0 0 1 _1 0 1 1 _1 0 >> 0 0 0 _1 _1 _1 _1 1 _1 >> 0 0 0 0 _1 1 0 0 1 >> 0 0 1 0 _1 _1 0 0 0 >> 0 0 1 _1 1 _1 1 0 1 >> >> The two columns of 0's are necessary for this to work correctly. >> >> Using place value and J we can "read" these numbers correctly >> >> f*($f)$|.2^i.9 >> >> 0 0 _64 32 16 0 _4 _2 1 >> 0 0 64 _32 0 8 4 _2 0 >> 0 0 0 _32 _16 _8 _4 2 _1 >> 0 0 0 0 _16 8 0 0 1 >> 0 0 64 0 _16 _8 0 0 0 >> 0 0 64 _32 16 _8 4 0 1 >> >> +/"1 f*($f)$|.2^i.9 >> _21 42 _59 _7 40 45 >> >> And with no change in code, #. Gives the same results >> >> #.f >> _21 42 _59 _7 40 45 >> >> It is only #: that must be changed. I would think this would be fairly >> straightforward if these representatives of negative binary numbers were >> adopted. >> >> #:#.f >> >> 1 1 1 1 1 0 0 >> 0 1 0 0 0 1 0 >> 0 1 1 1 1 0 1 >> 1 0 1 0 0 1 0 >> 0 1 1 1 0 1 1 >> 1 1 0 0 0 1 0 >> >> Since the current representation that #: provides has only positive numbers >> it is where the changes are necessary. >> >> Finally you would never see the displays above if you only consider positive >> integers. >> >> ]e=:#:i.7 >> 0 0 0 >> 0 0 1 >> 0 1 0 >> 0 1 1 >> 1 0 0 >> 1 0 1 >> 1 1 0 >> #.e >> 0 1 2 3 4 5 6 >> >> So, my take on this is that the user must learn to accept a new way of >> understanding of what binary numbers look like. They will be unique. You >> can identify the sign by the leading element. If you can read the number in >> binary, it will be the same number but negative. >> >> I keep trying to explain, and as I do it is getting easier for me to >> understand what I am saying. That's I guess how you learn, by trying to >> teach. >> >> Linda > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm