Velocity Reviews > Can we replace 8 bits by 2 bits?

# Can we replace 8 bits by 2 bits?

Umesh
Guest
Posts: n/a

 01-05-2007
This is a basic thing.
Say A=0100 0001 in ASCII which deals with 256 characters(you know
better than me!)
But we deal with only four characters and 2 bits are enough to encode
them. I want to confirm if we can encode A in 2bits(say 00), B in 2
bits (01), C in 2 bits(10) and D in 2 bits by some program. I only use
this four alphabet in my work. Can u pl write a sample program to reach
my goal?

jacob navia
Guest
Posts: n/a

 01-05-2007
Umesh a écrit :
> This is a basic thing.
> Say A=0100 0001 in ASCII which deals with 256 characters(you know
> better than me!)
> But we deal with only four characters and 2 bits are enough to encode
> them. I want to confirm if we can encode A in 2bits(say 00), B in 2
> bits (01), C in 2 bits(10) and D in 2 bits by some program. I only use
> this four alphabet in my work. Can u pl write a sample program to reach
> my goal?
>

Dear customer

We are ready to fulfill your request, and we thank you for the
confidence you give us by placing your order.

card payments are accepted we will gladly send you the requested
program.

Yours sincerely

J.K. OB

Customer support.

P.S.
www.DoMyHomework.com

Ondra Holub
Guest
Posts: n/a

 01-05-2007
Umesh napsal:
> This is a basic thing.
> Say A=0100 0001 in ASCII which deals with 256 characters(you know
> better than me!)
> But we deal with only four characters and 2 bits are enough to encode
> them. I want to confirm if we can encode A in 2bits(say 00), B in 2
> bits (01), C in 2 bits(10) and D in 2 bits by some program. I only use
> this four alphabet in my work. Can u pl write a sample program to reach
> my goal?

Yes, you can encode it this way, but you would have problem to work
with it. For example accessing 6th character of such array would be
something like (I did not test following code):

char GetNthChar(const char array[] a, size_t index)
{
const size_t im4 = index % 4 * 2;
return (a[index / 4] & (3 << im4)) >> im4;
}

However it may be usefull to store your data in such format. So I would
recommend to encode it before storing, decode it after loading and work
with ordinary char array containing values 0, 1, 2 and 4 only.

If you really need to save couple of bytes, you should write some
wrapping class which overloads operator[] and hides all these bit
operations. It would be something like std::vector<bool> not for 1 bit
values, but for 2 bit values.

osmium
Guest
Posts: n/a

 01-05-2007
"Umesh" wrote:

> Say A=0100 0001 in ASCII which deals with 256 characters(you know
> better than me!)
> But we deal with only four characters and 2 bits are enough to encode
> them. I want to confirm if we can encode A in 2bits(say 00), B in 2
> bits (01), C in 2 bits(10) and D in 2 bits by some program. I only use
> this four alphabet in my work. Can u pl write a sample program to reach
> my goal?

The switch statement *may* be germane to your problem.

BTW, ASCII does not deal with 256 characters, ASCII consists of only 128
characters. Terminology is a bitch since it is often used improperly by the
very people who in fact know better. It's "quicker" that way.

Umesh
Guest
Posts: n/a

 01-05-2007
Suppose that I define an array of 2 bits {00,01,10,11} . Now when the
program finds A in the text file it replaces with 00, B with 01, C with
10 and D with 11. So the encoded file will take 1/8 of the space of the
original file.

During decoding I'll replace 00 by A, 01 by B, 10 by C and 11 by D to
regain the original file.

I'm an inexperienced programmer. Pl help.

Lew Pitcher
Guest
Posts: n/a

 01-05-2007

Umesh wrote:
> Suppose that I define an array of 2 bits {00,01,10,11} . Now when the
> program finds A in the text file it replaces with 00, B with 01, C with
> 10 and D with 11. So the encoded file will take 1/8 of the space of the
> original file.
>
> During decoding I'll replace 00 by A, 01 by B, 10 by C and 11 by D to
> regain the original file.
>
> I'm an inexperienced programmer. Pl help.

Your question doesn't really belong in comp.lang.c
someone to teach you the rudiments of the skill of writing programs.

Write the code to do this:

open the input file
open the output file
for each character in the input file
if the character is 'A'
write binary 00 to the output file
else if the character is 'B'
write binary 01 to the output file
else if the character is 'C'
write binary 10 to the output file
else if the character is 'D'
write binary 11 to the output file
else
do nothing
end-if
end-if
end-if
end-if
end-for-loop
close the output file
close the input file
terminate the program

HTH
--
Lew

Richard Heathfield
Guest
Posts: n/a

 01-05-2007
Umesh said:

> This is a basic thing.
> Say A=0100 0001 in ASCII which deals with 256 characters

128, actually, but I know what you mean. Certainly 8 bits are sufficient to
encode 256 characters, which is what you actually care about.

> But we deal with only four characters and 2 bits are enough to encode
> them. I want to confirm if we can encode A in 2bits(say 00), B in 2
> bits (01), C in 2 bits(10) and D in 2 bits by some program. I only use
> this four alphabet in my work. Can u pl write a sample program to reach
> my goal?

Here's some code to split a byte into four:

void decode(char *letter, int ch)
{
const char alphabet[] = "ABCD";

for(i = 0; i < 4; i++)
{
letter[i] = alphabet[(ch & mask) >> (i * 2)];
}
}

letter must point to the first element in an array of at least four chars.
Note that decode() does not build a string. If you want a string, deal with
the null terminator yourself.

If you are decoding, say, 0xAD, this is 10101101 in binary, and at the end
of the decoding process letter[0] will store 'C', letter[1] will store 'C',
letter[2] will store 'D', and letter[3] will store 'B'.

Encoding is quite easy too. Simply reverse the process. For decoding,
though, you may find it convenient to have an alphabet array of UCHAR_MAX +
1 bytes, all of which have the value 0, but set alphabet['A'] to 1,
alphabet['B'] to 2, alphabet['C'] to 3, and alphabet['D'] to 4. Then you
can say: if(alphabet[letter[i]] == 0) { error - invalid code } else { your
OR-mask is alphabet[letter[i]] - 1 so you can OR it into your encoding, and

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.

Richard Heathfield
Guest
Posts: n/a

 01-05-2007
Umesh said:

> Suppose that I define an array of 2 bits {00,01,10,11} . Now when the
> program finds A in the text file it replaces with 00, B with 01, C with
> 10 and D with 11. So the encoded file will take 1/8 of the space of the
> original file.

A quarter.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.

Umesh
Guest
Posts: n/a

 01-05-2007

> Your question doesn't really belong in comp.lang.c
> someone to teach you the rudiments of the skill of writing programs.
>
> Write the code to do this:
>
> open the input file
> open the output file
> for each character in the input file
> if the character is 'A'
> write binary 00 to the output file
> else if the character is 'B'
> write binary 01 to the output file
> else if the character is 'C'
> write binary 10 to the output file
> else if the character is 'D'
> write binary 11 to the output file
> else
> do nothing
> end-if
> end-if
> end-if
> end-if
> end-for-loop
> close the output file
> close the input file
> terminate the program
>
> HTH
> --
> Lew

Dear Lew,
I'm not asking you to put down the algorithm which I already did. I
want you to write a part of the program. I won't ask you to do that
once I learn C well. Because I've only learnt to make algorithms and
determine their time complexity, I need your help.

I heard that it is easier to implement programs than to make effective
algorithm which I already did. Actually my original algo is far more
complex than this. But I want to start from the simple one. Becuse I've
none to teach me, I think sometimes a little help comes handy. That's
why I'm here. I hope that expert folks like you won't upset me. Thank
you. I look forward to hear from you again. God bless you.

jacob navia
Guest
Posts: n/a

 01-05-2007
Umesh a écrit :
>>Your question doesn't really belong in comp.lang.c
>>someone to teach you the rudiments of the skill of writing programs.
>>
>>Write the code to do this:
>>
>> open the input file
>> open the output file
>> for each character in the input file
>> if the character is 'A'
>> write binary 00 to the output file
>> else if the character is 'B'
>> write binary 01 to the output file
>> else if the character is 'C'
>> write binary 10 to the output file
>> else if the character is 'D'
>> write binary 11 to the output file
>> else
>> do nothing
>> end-if
>> end-if
>> end-if
>> end-if
>> end-for-loop
>> close the output file
>> close the input file
>> terminate the program
>>
>>HTH
>>--
>>Lew

>
>
> Dear Lew,
> I'm not asking you to put down the algorithm which I already did. I
> want you to write a part of the program. I won't ask you to do that
> once I learn C well. Because I've only learnt to make algorithms and
> determine their time complexity, I need your help.
>

Lew is right.

You will NOT learn until you practice. And you will not practice
if somebody else does the work for you.

You must learn the basics first. Buy the book from Kernighan and
Ritchie, learn it, and then ask questions. I learned C that way,
and there wasn't any body else there to ask questions. It is
perfectly doable if you WORK.

Or pay a class in computer programming. That is possible too.

But we can't replace a teacher or a book, and we will not work