Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>jonas.thornvall@gmail.com writes:
>>Is it possible to create an object holding holding 1 bit, 2 bit, 3 bit
>>that still can be treated as a numerical value that can be added and
>>subtracted, without bit operations?
>No.
An attempt at implementing an object »holding« 8 bit:
function Byte( init )
{ "use strict";
this.value = (init|0) % 256;
if( !Byte.prototype.add )
{ Byte.prototype.add =
function( other )
{ this.value =
( (this.value|0) + (other.value|0) )%256; return this; };
Byte.prototype.valueOf =
function(){ return (this.value|0) % 256; };
Byte.prototype.toString =
function()
{ return 0,
Number.prototype.toString.call( (this.value|0) % 256, 10 ); }; }}
console.log( +new Byte( 130 ).add( new Byte( 130 )) );
4
Ok, I used the bit operation »|«, but the example would
still print »4« when »|0« is being deleted everywhere,
it was just a layer of additional security.