我们可以用2位替换8位吗? [英] Can we replace 8 bits by 2 bits?

查看:58
本文介绍了我们可以用2位替换8位吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个基本的东西。

用ASCII代码A = 0100 0001处理256个字符(你知道比我好b $ b!)

但是我们只处理四个字符而且2位足以编码它们的b $ b。我想确认我们是否可以用2比特(比如00)编码A,B比2用2比特(01)编码,C用2比特(10)编码,D用2比特编码。在我的作品中,我只使用了这个四个字母的

。您可以编写一个示例程序来达到我的目标吗?

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?

推荐答案

Umeshaécrit:
Umesh a écrit :

这是一个基本的东西。

用ASCII代码A = 0100 0001处理256个字符(你知道

比我好!)

但是我们只处理4个字符,2位足以编码

它们。我想确认我们是否可以用2比特(比如00)编码A,B比2用2比特(01)编码,C用2比特(10)编码,D用2比特编码。在我的作品中,我只使用了这个四个字母的

。你可以写一个示例程序来达到

我的目标吗?
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在我们的网站上。当您的VISA

卡付款被接受后,我们很乐意向您发送所需的

计划。


您诚挚的


JK OB


客户支持。


PS

我们的网站地址:
www.DoMyHomework.com
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


Umesh napsal:
Umesh napsal:

这是一个基本的东西。

用ASCII代码A = 0100 0001处理256个字符(你知道

比我好!)

但是我们只处理四个字符而且2位足以编码

它们。我想确认我们是否可以用2比特(比如00)编码A,B比2用2比特(01)编码,C用2比特(10)编码,D用2比特编码。在我的作品中,我只使用了这个四个字母的

。您可以编写一个示例程序来实现我的目标吗?
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?



是的,你可以用这种方式对它进行编码,但你可能会遇到问题

。例如,访问这样的数组的第6个字符将是

之类的东西(我没有测试下面的代码):


char GetNthChar(const char array [] a ,size_t index)

{

const size_t im4 = index%4 * 2;

return(a [index / 4]&( 3<< im4))> im4;

}


但是以这种格式存储数据可能很有用。所以我会建议在存储之前对它进行编码,在加载和工作后对其进行解码。

只包含值为0,1,2和4的普通字符数组。


如果你真的需要保存几个字节,你应该写一些

包装类,它会重载operator []并隐藏所有这些位

操作。它将类似于std :: vector< boolnot for 1 bit

值,但是对于2位值。

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<boolnot for 1 bit
values, but for 2 bit values.


这篇关于我们可以用2位替换8位吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆