[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Make own type (object) type in javascript.

JT

6/6/2015 10:12:00 PM

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?

And if so can that type be stored within an array.

I am interested in make type dependent upon base used. So if base 256 would create an 8 bit type, allocated and used in array. 128 an 7 bit type and so on.

Is it possible?
11 Answers

Ben Bacarisse

6/6/2015 11:58:00 PM

0

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.

<snip>
--
Ben.

ram

6/7/2015 12:14:00 AM

0

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.

Ben Bacarisse

6/7/2015 12:33:00 AM

0

ram@zedat.fu-berlin.de (Stefan Ram) writes:

> 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:

It seems unlikely that he meant can one store small whole numbers in
64-bit floating point variables. He's been doing that for months
(years?).

<snip>
--
Ben.

JT

6/7/2015 12:38:00 AM

0

Den söndag 7 juni 2015 kl. 02:33:32 UTC+2 skrev Ben Bacarisse:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>
> > 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:
>
> It seems unlikely that he meant can one store small whole numbers in
> 64-bit floating point variables. He's been doing that for months
> (years?).
>
> <snip>
> --
> Ben.

You are correct Ben, i want a type holding just X bits depending upon the chosen base.

Thomas 'PointedEars' Lahn

6/7/2015 12:39:00 AM

0

Ben Bacarisse wrote:

> 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.

var o = {
valueOf: function () {
return (+!!this.bit1) * 4 + (+!!this.bit2) * 2 + (+!!this.bit1);
}
};

o.bit2 = 23;

/* 42 */
40 + o

/* 23 */
25 - o

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Thomas 'PointedEars' Lahn

6/7/2015 12:40:00 AM

0

Ben Bacarisse wrote:

> 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.

var o = {
valueOf: function () {
return (+!!this.bit3) * 4 + (+!!this.bit2) * 2 + (+!!this.bit1);
}
};

o.bit2 = 23;

/* 42 */
40 + o

/* 23 */
25 - o

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

JT

6/7/2015 12:51:00 AM

0

Den söndag 7 juni 2015 kl. 02:33:32 UTC+2 skrev Ben Bacarisse:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>
> > 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:
>
> It seems unlikely that he meant can one store small whole numbers in
> 64-bit floating point variables. He's been doing that for months
> (years?).
>
> <snip>
> --
> Ben.

But what is this Ben?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_...

Typed Arrays are new to JavaScript with ECMAScript Edition 6 and present an array-like view of an underlying binary data buffer. The following table helps you to find the equivalent C data types:

TypedArray objects
Type Size Description Web IDL type Equivalent C type
Int8Array 1 8-bit twos complement signed integer byte int8_t
Uint8Array 1 8-bit unsigned integer octet uint8_t
Uint8ClampedArray 1 8-bit unsigned integer (clamped) octet uint8_t
Int16Array 2 16-bit twos complement signed integer short int16_t
Uint16Array 2 16-bit unsigned integer unsigned short uint16_t
Int32Array 4 32-bit twos complement signed integer long int32_t
Uint32Array 4 32-bit unsigned integer unsigned long uint32_t
Float32Array 4 32-bit IEEE floating point number unrestricted float float
Float64Array 8 64-bit IEEE floating point number unrestricted double double

JT

6/7/2015 1:03:00 AM

0

Den söndag 7 juni 2015 kl. 02:50:36 UTC+2 skrev jonas.t...@gmail.com:
> Den söndag 7 juni 2015 kl. 02:33:32 UTC+2 skrev Ben Bacarisse:
> > ram@zedat.fu-berlin.de (Stefan Ram) writes:
> >
> > > 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:
> >
> > It seems unlikely that he meant can one store small whole numbers in
> > 64-bit floating point variables. He's been doing that for months
> > (years?).
> >
> > <snip>
> > --
> > Ben.
>
> But what is this Ben?
>
> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_...
>
> Typed Arrays are new to JavaScript with ECMAScript Edition 6 and present an array-like view of an underlying binary data buffer. The following table helps you to find the equivalent C data types:
>
> TypedArray objects
> Type Size Description Web IDL type Equivalent C type
> Int8Array 1 8-bit twos complement signed integer byte int8_t
> Uint8Array 1 8-bit unsigned integer octet uint8_t
> Uint8ClampedArray 1 8-bit unsigned integer (clamped) octet uint8_t
> Int16Array 2 16-bit twos complement signed integer short int16_t
> Uint16Array 2 16-bit unsigned integer unsigned short uint16_t
> Int32Array 4 32-bit twos complement signed integer long int32_t
> Uint32Array 4 32-bit unsigned integer unsigned long uint32_t
> Float32Array 4 32-bit IEEE floating point number unrestricted float float
> Float64Array 8 64-bit IEEE floating point number unrestricted double double

Is it not possible to make ones own view of the array data buffer, must one use the ones above?

JT

6/7/2015 1:08:00 AM

0

Den söndag 7 juni 2015 kl. 03:03:24 UTC+2 skrev jonas.t...@gmail.com:
> Den söndag 7 juni 2015 kl. 02:50:36 UTC+2 skrev jonas.t...@gmail.com:
> > Den söndag 7 juni 2015 kl. 02:33:32 UTC+2 skrev Ben Bacarisse:
> > > ram@zedat.fu-berlin.de (Stefan Ram) writes:
> > >
> > > > 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:
> > >
> > > It seems unlikely that he meant can one store small whole numbers in
> > > 64-bit floating point variables. He's been doing that for months
> > > (years?).
> > >
> > > <snip>
> > > --
> > > Ben.
> >
> > But what is this Ben?
> >
> > https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_...
> >
> > Typed Arrays are new to JavaScript with ECMAScript Edition 6 and present an array-like view of an underlying binary data buffer. The following table helps you to find the equivalent C data types:
> >
> > TypedArray objects
> > Type Size Description Web IDL type Equivalent C type
> > Int8Array 1 8-bit twos complement signed integer byte int8_t
> > Uint8Array 1 8-bit unsigned integer octet uint8_t
> > Uint8ClampedArray 1 8-bit unsigned integer (clamped) octet uint8_t
> > Int16Array 2 16-bit twos complement signed integer short int16_t
> > Uint16Array 2 16-bit unsigned integer unsigned short uint16_t
> > Int32Array 4 32-bit twos complement signed integer long int32_t
> > Uint32Array 4 32-bit unsigned integer unsigned long uint32_t
> > Float32Array 4 32-bit IEEE floating point number unrestricted float float
> > Float64Array 8 64-bit IEEE floating point number unrestricted double double
>
> Is it not possible to make ones own view of the array data buffer, must one use the ones above?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Object...

Ben Bacarisse

6/7/2015 9:20:00 AM

0

jonas.thornvall@gmail.com writes:

> Den söndag 7 juni 2015 kl. 02:33:32 UTC+2 skrev Ben Bacarisse:
>> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>
>> > 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:
>>
>> It seems unlikely that he meant can one store small whole numbers in
>> 64-bit floating point variables. He's been doing that for months
>> (years?).
>>
>> <snip>
>> --
>> Ben.
>
> But what is this Ben?
>
> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_...

Can you rely on Edition 6? I may have misunderstood your goal which I
took to be a request for minimal sized objects. If you don't mind
wasting bits, there are lots of ways to do what you ask (see other
replies prompted by my "no" reply for some examples). But these new
types only give you another kind of container into which to pack the
data -- they don't allow you to do anything you could not do otherwise.

<snip>
--
Ben.