↑ ↓ ← → "C. Sauls" <ibisbasenji yahoo.com>
writes:
Is it possible, or could be made possible, to cast an integral type to
an array of bits, and vice versa? Ie:
int i = 123;
bit[] bs = cast(bit[]) i;
// bs == 0000 0000 0000 0000 0000 0000 0111 1011
int i2 = cast(int) bs;
// i2 == 123
An example of how I think this could be useful:
const char[] hexdigits = "0123456789ABCDEF";
char[] toHexLiteral(int i)
{
bit[] buf = cast(bit[]) i;
int nibel;
char[] ret;
for (int idx = 0; idx < buf.length; idx += 4)
{
nibel = cast(int) buf[idx..idx+4];
ret ~= hexdigits[nibel];
}
return ret;
}
Would it not be possible to implement this by duplicating the int's
memory into the storage for the bit[]? Might be something I don't know
about that prevents it. Just a random thought.
-- C. Sauls
-- Invironz
↑ ↓ ← → J Anderson <REMOVEanderson badmama.com.au>
writes:
C. Sauls wrote:
Is it possible, or could be made possible, to cast an integral type to
an array of bits, and vice versa? Ie:
int i = 123;
bit[] bs = cast(bit[]) i;
// bs == 0000 0000 0000 0000 0000 0000 0111 1011
int i2 = cast(int) bs;
// i2 == 123
An example of how I think this could be useful:
const char[] hexdigits = "0123456789ABCDEF";
char[] toHexLiteral(int i)
{
bit[] buf = cast(bit[]) i;
int nibel;
char[] ret;
for (int idx = 0; idx < buf.length; idx += 4)
{
nibel = cast(int) buf[idx..idx+4];
ret ~= hexdigits[nibel];
}
return ret;
}
Would it not be possible to implement this by duplicating the int's
memory into the storage for the bit[]? Might be something I don't
know about that prevents it. Just a random thought.
machines, there probably wouldn't be a need for copying.
-- C. Sauls
-- Invironz
↑ ↓ ← → "Sean L. Palmer" <palmer.sean verizon.net>
writes:
I asked for this a while back. I'd post a link but by god the web interface
for the D ng sucks hard.
One thing about this is that it could potentially replace bitfields.
(instead of a bitfield you'd have a constant slice of some bit array)
Sean
"C. Sauls" <ibisbasenji yahoo.com> wrote in message
news:bs0qdh$112i$1 digitaldaemon.com...
Is it possible, or could be made possible, to cast an integral type to
an array of bits, and vice versa? Ie:
int i = 123;
bit[] bs = cast(bit[]) i;
// bs == 0000 0000 0000 0000 0000 0000 0111 1011
int i2 = cast(int) bs;
// i2 == 123
An example of how I think this could be useful:
const char[] hexdigits = "0123456789ABCDEF";
char[] toHexLiteral(int i)
{
bit[] buf = cast(bit[]) i;
int nibel;
char[] ret;
for (int idx = 0; idx < buf.length; idx += 4)
{
nibel = cast(int) buf[idx..idx+4];
ret ~= hexdigits[nibel];
}
return ret;
}
Would it not be possible to implement this by duplicating the int's
memory into the storage for the bit[]? Might be something I don't know
about that prevents it. Just a random thought.
-- C. Sauls
-- Invironz
↑ ↓
← → "C. Sauls" <ibisbasenji yahoo.com>
writes:
C. Sauls wrote:
const char[] hexdigits = "0123456789ABCDEF";
char[] toHexLiteral(int i)
{
bit[] buf = cast(bit[]) i;
int nibel;
char[] ret;
for (int idx = 0; idx < buf.length; idx += 4)
{
nibel = cast(int) buf[idx..idx+4];
ret ~= hexdigits[nibel];
}
return ret;
}
Out of fairness I thought I should show how I currently have this
implemented, since its not too complex as is. I just think that
int->bit[],bit[]->int has some potential.
Current toHexLiteral():
// begin snippet
const char[] hexdigits = "0123456789abcdef";
char[] toHexLiteral(uint foo)
{
char[] ret;
for (int i = 28; i >= 0; i -= 4)
{
ret ~= hexdigits[cast(ubyte)(foo >> i) & 0x0f];
}
return ret;
}
// end snippet
It more-or-less does the same thing, but includes all the fun and joy of
bit-shifting. I think the bit[] indexing would be instructionally
simpler. Could be wrong. Wouldn't be the first time. :D
-- C. Sauls
-- Invironz
↑ ↓ ← → Alix Pexton <Alix thedjournal.com>
writes:
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
I think that the attached code should do something like a cast to and
from a bit array, using templates, so it will work for more types than
just uint, but what you get will not be that usefull for signed types,
floats, pointers, arrays, references. But the process could be built
upon to do more interesting things. I haven't tested it, so don't cry
foul if it doesn't work, though I don't see why it souldn't work...
Alix...
C. Sauls wrote:
C. Sauls wrote:
const char[] hexdigits = "0123456789ABCDEF";
char[] toHexLiteral(int i)
{
bit[] buf = cast(bit[]) i;
int nibel;
char[] ret;
for (int idx = 0; idx < buf.length; idx += 4)
{
nibel = cast(int) buf[idx..idx+4];
ret ~= hexdigits[nibel];
}
return ret;
}
Out of fairness I thought I should show how I currently have this
implemented, since its not too complex as is. I just think that
int->bit[],bit[]->int has some potential.
Current toHexLiteral():
// begin snippet
const char[] hexdigits = "0123456789abcdef";
char[] toHexLiteral(uint foo)
{
char[] ret;
for (int i = 28; i >= 0; i -= 4)
{
ret ~= hexdigits[cast(ubyte)(foo >> i) & 0x0f];
}
return ret;
}
// end snippet
It more-or-less does the same thing, but includes all the fun and joy of
bit-shifting. I think the bit[] indexing would be instructionally
simpler. Could be wrong. Wouldn't be the first time. :D
-- C. Sauls
-- Invironz
--
Alix Pexton
Webmaster - http://www.theDjournal.com
Alix theDjournal.com
↑ ↓ ← → Chris Sauls <ibisbasenji yahoo.com>
writes:
Alix Pexton wrote:
> template TBitMask(T){
public alias bit[T.size * 8] bitMask;
private union masker{
bitMask bits;
T type;
}
public bitMask typeToMask(in T t){
masker m;
m.type = t;
return m.bits;
}
public T maskToType(in bitMask bm){
masker m;
m.bits = bm;
return m.type;
}
}
It does indeed work... and it leads me to ressurect another subject we
brought up once some while back: persistance. I've done a little
experimenting with this template of yours and found that it could very
well be used to build a nice persisting module! I'll post my own
experiment here sometime soon, once I clear up a couple of bugs and add
a lovely packaging feature I'm wanting. :)
-- C. Sauls
-- Invironz
↑ ↓ ← → Alix Pexton <Alix thedjournal.com>
writes:
I'm very pleased it worked, that it has been helpful, and that it has
inspired you.
Alix...
Chris Sauls wrote:
Alix Pexton wrote:
> template TBitMask(T){
public alias bit[T.size * 8] bitMask;
private union masker{
bitMask bits;
T type;
}
public bitMask typeToMask(in T t){
masker m;
m.type = t;
return m.bits;
}
public T maskToType(in bitMask bm){
masker m;
m.bits = bm;
return m.type;
}
}
It does indeed work... and it leads me to ressurect another subject we
brought up once some while back: persistance. I've done a little
experimenting with this template of yours and found that it could very
well be used to build a nice persisting module! I'll post my own
experiment here sometime soon, once I clear up a couple of bugs and
add a lovely packaging feature I'm wanting. :)
-- C. Sauls
-- Invironz
--
Alix Pexton
Webmaster - http://www.theDjournal.com
Alix theDjournal.com