C#:如何位移十六进制数字(C#: How to bit shift hexadecimal digits)

编程入门 行业动态 更新时间:2024-10-23 19:32:33
C#:如何位移十六进制数字(C#: How to bit shift hexadecimal digits)

好的,我正在开发一个卡片播放程序,并将卡片值存储为十六进制数字。 这是数组:

public int[] originalCards = new int[54] { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x50, 0x51 };

第一位数字代表套装(1 =黑桃; 2 =棍棒; .... 5 =小王子)第二位数字是卡片的编号(1 = ace,5 = 5; 13 = K等)。

我想要做如下的事情:

伪代码:

public int ReturnCard(int num) { int card = currentDeck[num]; int suit = card.firsthexdigit; int value = card.secondhexdigit; return 0; }

我不需要一种新的方法来处理整数,为了清楚起见,我只是将其包含在内。

任何人都知道如何在C#中做到这一点?

编辑:好的,我正在使用位移,如其中一个答案中所述。 我可以得到第二个数字(西装)就好了,但第一个数字一直以'0'出现。 任何想法为什么?

编辑:编辑:好的,现在工作正常。 多谢你们。

Okay, I am working on a card playing program, and I am storing card values as hexadecimal digits. Here is the array:

public int[] originalCards = new int[54] { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x50, 0x51 };

The first digit refers to the suit (1 = spades; 2 = clubs; .... 5 = Jokers) The second digit is the number of the card (1 = ace, 5 = 5; 13 = K, etc).

I would like to do something like the following:

Pseudocode:

public int ReturnCard(int num) { int card = currentDeck[num]; int suit = card.firsthexdigit; int value = card.secondhexdigit; return 0; }

I don't need a new method to work on ints, I just included it for clarity's sake.

Anybody know how to do this in C#?

Edit: Okay, I am using bit shifting as described in one of the answers. I can get the second digit (the suit) just fine, but the first digit keeps coming out as '0'. Any idea why?

Edit:edit: okay, works fine now. Thanks guys.

最满意答案

这是使用位域的答案。

struct { unsigned int suit:4; unsigned int value:4; } card = currentDeck[num]; int suit = card.suit; int value = card.value;

您可能需要为填充添加int作为第一个或最后一个字段,以便正确排列比特。 位字段通常用于访问硬件,因为硬件寄存器经常将多个标志打包为单个字节。

顺便说一下,如果您使用位移,您想要移位十六进制数字中的位数。 一个十六进制数字保存值0-15或0-F,这需要4位而不是8.所以这应该被使用:

int suit = (card & 0xF0) >> 4;

Here's an answer using bit fields.

struct { unsigned int suit:4; unsigned int value:4; } card = currentDeck[num]; int suit = card.suit; int value = card.value;

You may need to add in int for padding as either the first or last field to line the bits up properly. Bit fields are normally used to access hardware because hardware registers frequently pack multiple flags into a single byte.

By the way if you use the bit shift, you want to shift by the number of bits in a hexadecimal digit. One hex digit holds values 0 - 15 or 0 - F, this requires 4 bits not 8. So this should be used:

int suit = (card & 0xF0) >> 4;

更多推荐

本文发布于:2023-08-06 19:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454991.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位移   数字   十六进制   bit   hexadecimal

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!