Bit Encoding design

G

Guest

I am woking on a base64 encoder and I am looking for some design help. I
have a woking model but would like some input on the design. I currently
read 3 bytes from a binary stream with each byte place in a seperate int32
var. Bytes are shifted to the left 0 8 or 16 places then the 3 vars are
"or"ed together t make a stream of bits. I then extract each 6 bit set
using a mask into a new int32 then shift them to the right to the fist byte
position. This seems to work OK but I am looking for other ideas to implment
this functionality.
 
T

Tom Shelton

I am woking on a base64 encoder and I am looking for some design help. I
have a woking model but would like some input on the design. I currently
read 3 bytes from a binary stream with each byte place in a seperate int32
var. Bytes are shifted to the left 0 8 or 16 places then the 3 vars are
"or"ed together t make a stream of bits. I then extract each 6 bit set
using a mask into a new int32 then shift them to the right to the fist byte
position. This seems to work OK but I am looking for other ideas to implment
this functionality.

Is there some reason not to use the functionality built into the
framework?

System.Convert.ToBase64String
System.Convert.FromBase64String
System.Convert.ToBase64CharArray
System.Convert.FromBase64CharArray

Just curious....

--
Tom Shelton [MVP]
Powered By Gentoo Linux 1.4
There are two major products that come out of Berkeley: LSD and UNIX.
We don't believe this to be a coincidence.
-- Jeremy S. Anderson
 
S

Shawn B.

If you must do this, use an unsigned int. There is already a Base64 utility
in System.Convert that does what you want. However, if, for your own
reasons, you need or want to do it you way still, look at my bit manipulator
class that may help you, search for "Shawn Bullock" on
www.planet-source-code.com in the .net section and you'll see a bitwise
value type in C# (not vb compatible). It overloads all the operators and
allows you to work with a value just like you do an int, but it help you a
little with bit manipulations.

Of course, you can also do this in C# (I strongly recommend using an
unsigned int or UInt32) just use the << and >> operators to shift and to or
them together use the | character.

For example,

A |= B

In the bitwise class I wrote, you can say

Binary32 a = "1100";
Binary32 b = "0001";

a |= b; // := "1101"

Of course, you'll get the same results with an unsigned int like this:

uint a = 12;
uint b = 1;

a |= b;

Etc, Etc, Etc.


The only thing the Binary class I wrote does that you can't do automatically
with a standard valuetype is rotate left or rotate rigth, and get/set
individual bits. For example,

Binary8 x = "11011101";
if (x[1] == true || x[2] == 1 || x[5] == "0") {
...
}
Binary8 y = "11011101";
y[3] = 1; // "11011111";

But there are other ways of going about getting the same information with a
uint.

I wrote that class because I wrote a 6502 processor simulator in C# and
needed these kinds of bit operations. It's fast, too. You get the source
code.

As it pertains to your original question, if you look at the source code,
you'll get a lot of insight into doing the kinds of bit manipulations you
desire for your own work in question.


Thanks,
Shawn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top