提取特定位并将其复制到C中的内存中的其他位置 [英] Extract specific bits and copy it to some other place in memory in C

查看:67
本文介绍了提取特定位并将其复制到C中的内存中的其他位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从内存中位位置'm'复制'n'位。我有一个指向32位对齐内存中某个块的指针。位置'm'是该指针的m位。我想将这个'm'位的n位复制到一个新的位'p',这是指针的'p'位。这也可能与'm'重叠。还需要处理这种情况。



例如,



< ---- 32 bit ----->

ptr-> | _________________ |

| _______ | m | _______ |

| _________________ |

| _________________ |

| __ | p | ____________ |

| _________________ |



我要提取67位从第45位位置(m)并将所有这些复制到第132位位置(p)。



我尝试了什么:



我不知道如何继续

I want to copy 'n' bits from bit position 'm' in memory. I have a pointer to a certain block in 32-bit aligned memory. Position 'm' is m bits from this pointer. I want to copy n bits from this 'm' bit position into a new bit position 'p', which is 'p' bits from the pointer. This might overlap with 'm' as well. Need to handle that scenario also.

For example,

<----32 bit----->
ptr->|_________________|
|_______|m|_______|
|_________________|
|_________________|
|__|p|____________|
|_________________|

I want to extract 67 bits from 45th bit position(m) and copy all those to 132nd bit position(p).

What I have tried:

I have no clue as to how to proceed

推荐答案

访问单个信息位是其中一个每个软件的最古老的任务,因此它是经验丰富的程序员的基础知识。花几个小时和几杯咖啡来理解它,因为对这个问题的理解是非常宝贵的,并且可以帮助你完成与数据或内存相关的任务。



In C语言是一个字节的最小内存块,通常用unsigned char的数据类型来描述。它有8位(0或1)值,所以它可以存储2 ^ 8 = 256个值。



要访问内存中的位,定义是有意义的一个指向其开始的字节指针,而不是使用偏移量来访问一个字节。在此字节中,您可以使用按位运算符访问位,如此精美文章中所述



C中一个不错但高级的东西是位字段。这是将单个位声明为变量的类型。请查看此文章
The access to single bits of information is one of the oldest tasks of every software, so it is base knowledge for an experienced programmer. Take some hours and some cups of coffee to understand it, because the understanding of this issue is very precious and will help you in every data or memory related task.

In the C-language is the smallest memory block a byte, often described with the data type of unsigned char. It has 8 bits (0 or 1) values, so it can stor 2^8 = 256 values.

To access a bit in a memory it makes sense to define a byte pointer to its start and than work with the offset, to access a byte. In this byte you access the bits with bitwise operators like it is explained in this fine article.

A nice but advanced thing in C are bit fields. That are types which declare single bits as variables. Take a look at this article.


这篇关于提取特定位并将其复制到C中的内存中的其他位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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