有趣的位游戏 [英] Funny bit games

查看:76
本文介绍了有趣的位游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。有一个任务是将一个int分成三个unsigned char'。

还有一个限制。给定int的最大大小是

10 ^ 9,只需17位就可以编码。那么,

问题是什么?如何将包含int的最多17个值位分成3个
无符号字符(8 + 8 + 1,6位)?问题是我不能使用

unsigned char指针,指向这样的int:


unsigned char * uchp;

unsigned int i = 1000000000;


uchp =& i;


for(int j = 0; j< sizeof(int); ++ j)

cout<< * uchp;


指向int的每个字节...在汇编程序中可以执行什么

使用BYTE PTR,架构是Intel x86。但是如何使用C ++将int分成

三个无符号字符?

解决方案

Isliguezze写道:


嗨。有一个任务是将一个int分成三个unsigned char'。

还有一个限制。给定int的最大大小是

10 ^ 9,只需17位就可以编码。那么,

问题是什么?如何将包含int的最多17个值位分成3个
无符号字符(8 + 8 + 1,6位)?



你是什么意思(8 + 8 + 1,6位)?如何将17位映射到三个字节的

位?


问题是我不能使用

unsigned char指针,指向这样的int:


unsigned char * uchp;

unsigned int i = 1000000000;


uchp =& i;


for(int j = 0; j< sizeof(int); ++ j)

cout<< * uchp;


指向int的每个字节...在汇编程序中可以执行什么

使用BYTE PTR,架构是Intel x86。但是如何使用C ++将int拆分为

三个无符号字符?



请参阅按位运算符AND(&)和右移运算符(>>)。

应用蒙版,然后转移。


V

-

请在通过电子邮件回复时删除资金''A' />
我没有回复最热门的回复,请不要问


查看按位运算符AND(&)和右移运算符(>>)。


应用蒙版,然后移动。



我需要什么面膜?什么是面膜?它是如何构建的?


" Isliguezze" <是******** @ gmail.com写的消息

news:83 ********************** ************ @ k13g2000 hse.googlegroups.com ...


嗨。有一个任务是将一个int分成三个unsigned char'。

还有一个限制。给定int的最大大小是

10 ^ 9,只需17位就可以编码。那么,

问题是什么?如何将包含int的最多17个值位分成3个
无符号字符(8 + 8 + 1,6位)?问题是我不能使用

unsigned char指针,指向这样的int:


unsigned char * uchp;

unsigned int i = 1000000000;


uchp =& i;


for(int j = 0; j< sizeof(int); ++ j)

cout<< * uchp;


指向int的每个字节...在汇编程序中可以执行什么

使用BYTE PTR,架构是Intel x86。但是如何使用C ++将int拆分为

三个无符号字符?



虽然这与你讨论的不一样,但使用工会打包/拆包8 b $ b位可以派上用场到时间:


-----


#include< iostream>


using namespace std;


void main(int argc,char * argv [])

{

typedef int XINT;

typedef char XBYTES [4];


union sigma

{

XINT xint;

XBYTES xbytes;

}测试;


test.xint =''ABCD'';

cout<< test.xbytes [0]<< endl;

cout<< test.xbytes [1]<< endl;

cout<< test.xbytes [2]<< endl;

cout<< test.xbytes [3]<<结束;

}


-----


IMO太少是有机会讨论工会的美德。虽然

我们的框架不超过8位(根据要求),但我们需要注意的是一些新技术。 (即''有趣的比赛'')


R.A. Nagy
http://www.Soft9000.com


Hi. There''s a task of splitting an int into three unsigned char''s.
There''s also one limitation. The maximum size of the given int is
10^9, what''s enough to code with as small as 17 bits. So, what is the
problem? How to split a maximum 17 value bits containing int into 3
unsigned chars(8+8+1, 6 bits)? The problem is that I can''t use
unsigned char pointer, to point on int like this:

unsigned char *uchp;
unsigned int i = 1000000000;

uchp = &i;

for (int j = 0; j < sizeof(int); ++j)
cout << *uchp;

to point on each byte of int... What can be performed in assembler
using BYTE PTR, architecture is Intel x86. But how to split int into
three unsigned chars using C++?

解决方案

Isliguezze wrote:

Hi. There''s a task of splitting an int into three unsigned char''s.
There''s also one limitation. The maximum size of the given int is
10^9, what''s enough to code with as small as 17 bits. So, what is the
problem? How to split a maximum 17 value bits containing int into 3
unsigned chars(8+8+1, 6 bits)?

What do you mean by "(8+8+1, 6 bits)"? How do the 17 bits map into the
bits of the three bytes?

The problem is that I can''t use
unsigned char pointer, to point on int like this:

unsigned char *uchp;
unsigned int i = 1000000000;

uchp = &i;

for (int j = 0; j < sizeof(int); ++j)
cout << *uchp;

to point on each byte of int... What can be performed in assembler
using BYTE PTR, architecture is Intel x86. But how to split int into
three unsigned chars using C++?

See the bitwise operator AND (&) and the right shift operator (>>).
Apply the mask, then shift.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


See the bitwise operator AND (&) and the right shift operator (>>).

Apply the mask, then shift.

What do I need the mask for? What is mask? How is it constructed?


"Isliguezze" <is********@gmail.comwrote in message
news:83**********************************@k13g2000 hse.googlegroups.com...

Hi. There''s a task of splitting an int into three unsigned char''s.
There''s also one limitation. The maximum size of the given int is
10^9, what''s enough to code with as small as 17 bits. So, what is the
problem? How to split a maximum 17 value bits containing int into 3
unsigned chars(8+8+1, 6 bits)? The problem is that I can''t use
unsigned char pointer, to point on int like this:

unsigned char *uchp;
unsigned int i = 1000000000;

uchp = &i;

for (int j = 0; j < sizeof(int); ++j)
cout << *uchp;

to point on each byte of int... What can be performed in assembler
using BYTE PTR, architecture is Intel x86. But how to split int into
three unsigned chars using C++?

While this is not the same as what you are discussing, packing / unpacking 8
bits using a union can come in handy from time to time:

-----

#include <iostream>

using namespace std;

void main(int argc, char *argv[])
{
typedef int XINT;
typedef char XBYTES[4];

union sigma
{
XINT xint;
XBYTES xbytes;
} test;

test.xint = ''ABCD'';
cout << test.xbytes[0] << endl;
cout << test.xbytes[1] << endl;
cout << test.xbytes[2] << endl;
cout << test.xbytes[3] << endl;
}

-----

IMO far too few is the opportunity to discuss the virture of unions. While
we are not framing more than 8 bits (as requested), the technique is one for
a few new-of-us to be aware of. (i.e. ''funny bit games'' :)

R.A. Nagy
http://www.Soft9000.com


这篇关于有趣的位游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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