Bit-based values/structures

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
mback2k
Enthusiast
Enthusiast
Posts: 257
Joined: Sun Dec 02, 2007 12:11 pm
Location: Germany

Bit-based values/structures

Post by mback2k »

It would be great if PB could support bit-based values/structures.

For example:

Code: Select all

Structure RX
  opcode.b
  StructureBit ri.b   ; byte representation (optional)
    r.b[4]            ; Big-Endian unsigned value with 4 bit length, max value = (2^4)-1
    i.b[4]            ; Big-Endian unsigned value with 4 bit length at 4 bit offset, max value = (2^4)-1
  EndStructureBit
  StructureBit bd.w   ; word representation (optional)
    b.b[4]            ; Big-Endian unsigned value with 4 bit length, max value = (2^4)-1
    d.b[12]           ; Big-Endian unsigned value with 12 bit length at 4 bit offset, max value = (2^12)-1
  EndStructureBit
EndStructure

Define MyValue.RX
MyValue\opcode = $72
MyValue\r = 15
MyValue\i = 0
MyValue\b = 0
MyValue\d = $FFF
.b would mean Big-Endian and .l Little-Endian.

I know that I can handle all this with bit shifting, but this new thing would make it a lot easier IMO.
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

.b and .l are already used - byte and long
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

DoubleDutch wrote:.b and .l are already used - byte and long
Indeed, maybe we should use .bit and .lbit then.
I like logic, hence I dislike humans but love computers.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

If it existed wouldn't it's take a form more like this?

Code: Select all

; .y = bit data type Big-Endian, .z = bit data type Little-Endian
Structure ri   ; byte representation (optional)
  r.y[4]            ; Big-Endian unsigned value with 4 bit length, max value = (2^4)-1
  i.y[4]            ; Big-Endian unsigned value with 4 bit length at 4 bit offset, max value = (2^4)-1
EndStructure

Structure bd   ; word representation (optional)
  b.y[4]            ; Big-Endian unsigned value with 4 bit length, max value = (2^4)-1
  D.y[12]           ; Big-Endian unsigned value with 12 bit length at 4 bit offset, max value = (2^12)-1
EndStructure

Structure RX
  StructureUnion
    bd.bd
    opcode.b
    ri.ri
  EndStructureUnion
EndStructure


Define MyValue.RX
MyValue\opcode = $72
MyValue\ri\r = 15
MyValue\ri\i = 0
MyValue\bd\b = 0
MyValue\bd\D = $FFF
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

I want a signed and an unsigned bit type ;)

(sorry, couldn't help myself)
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

Maybe it should be kept simple like a stream of bits, then the Endian applied when the structure is used, this way it could be applied to existing data types.

Code: Select all

Structure  pattern
  r.bit[4]            ; value with 4 bit length, max value = (2^4)-1 
  i.bit[4]            ; value with 4 bit length at 4 bit offset, max value = (2^4)-1 
EndStructure 
settings.pattern ; this uses the default mode on the processor - eg little endian on x86, big on 68k

settings2.<pattern ; forces big endian
settings3.>pattern ; forces little endian

count.<l ; forces big endian long store
count2.>l ; forces little endian long store

name$ ; normal string store for all processors is forward
name2<$ ; force big endian string store
name3>$ ; force little endian string store

Nice side effect: a union of little and big string types would allow you to read the same string in reverse using a different variable name.

This should also be possible:

Code: Select all

Structure  pattern
  r.<bit[4]            ; value with 4 bit length, max value = (2^4)-1 
  i.>bit[4]            ; value with 4 bit length at 4 bit offset, max value = (2^4)-1 
EndStructure 
To store bits reverse order.
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
mback2k
Enthusiast
Enthusiast
Posts: 257
Joined: Sun Dec 02, 2007 12:11 pm
Location: Germany

Post by mback2k »

DoubleDutch wrote:.b and .l are already used - byte and long
Yep, but that doesn't mean they can't be re-used in another context ;)

Nice ideas, keep it coming :)
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

I think that currently:

Code: Select all

r.b[4] 
Is a fixed array of 4 bytes:
r[0] to r[3]

imho Using .b for anything else would just be confusing.
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

could anyone explain to me with one, two sentences, what the difference between a "big endian bit" and a "little endian bit" should be?

a bit is a bit, isn't it?
oh... and have a nice day.
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

more than one or two sentences, but ok.

I gets me a bit confused.
it sais, x86 uses little-endian for bits, you say the same,
but the SHIFT commands ( >>, << ) are big-endian formatted...?

*scratch*think*

... byte order in memory is definitely l-e, so seems the shift commands are only this way for better imagination.

it's convention for a long time, ASM shifts always were written b-e, like numbers are written.
maybe that's the only reason...

; . . .

I agree that .b and .l should not be reused.
also, when [ ] should be used, the meaning should stay the same as used to:
a static array of this type inside the struct.
oh... and have a nice day.
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

Once inside the data registers, it's big endian - its when its written to or read from memory that the x86 goes to little endian.

Historically (I think) it's to maintain easy reading of data written by a processor with a lower data bit size.
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Post Reply