Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can we replace 8 bits by 2 bits?

Reply
Thread Tools

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?

 
Reply With Quote
 
 
 
 
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.

Please buy products for US$ 200 at our website. When your VISA
card payments are accepted we will gladly send you the requested
program.

Yours sincerely

J.K. OB

Customer support.

P.S.
Our website address:
www.DoMyHomework.com
 
Reply With Quote
 
 
 
 
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.

 
Reply With Quote
 
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.


 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      01-05-2007
[cross-posting deleted]

Ondra Holub wrote:
> 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?

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


How about std::bitset?

Cheers! --M

 
Reply With Quote
 
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.

 
Reply With Quote
 
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
You aren't asking about a C feature or problem, you are asking for
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

 
Reply With Quote
 
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";
int mask = 0x11;

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

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
then shift left ready for the next mask }

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
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.
 
Reply With Quote
 
Umesh
Guest
Posts: n/a
 
      01-05-2007

> Your question doesn't really belong in comp.lang.c
> You aren't asking about a C feature or problem, you are asking for
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can change UART data port from 8 bits to 16 bits? Zhi VHDL 2 10-09-2007 04:23 AM
Can we replace 8 bits by 2 bits? Umesh C Programming 22 01-10-2007 12:59 AM
8 bits/ch vs 16 bits/ch in PS Terry Digital Photography 5 01-21-2004 06:59 PM
8-Bits vs 12 or 16 bits/pixel; When does more than 8 bits count ? Al Dykes Digital Photography 3 12-29-2003 07:08 PM
win XP 32 bits on a 64 bits processor.. Abbyss Computer Support 3 11-13-2003 12:39 AM



Advertisments