Position of rightmost set bit

本文介绍了一种高效的方法来确定一个整数二进制表示中从右向左计数的第一个1的位置。该方法通过取整数的补码、与原始整数进行按位与操作、再对结果取对数并加1来实现。

reference: 

http://www.geeksforgeeks.org/position-of-rightmost-set-bit/


Problem Definition:

Write a one line C function to return position of first 1 from right to left, in binary representation of an Integer.


Solution:

Example 18(010010)
1. Take two's complement(i.e. -n) of the given no as all bits are reverted
except the first '1' from right to left (10111)

2  Do an bit-wise & with original no, this will return no with the
required one only (00010)

3  Take the log2 of the no, you will get position -1 (1)

4  Add 1 (2)


Code:

unsigned int getFirstSetBitPos(int n)
{
   return log2(n&-n)+1;//or log2(n & (n^(n-1)))+1
}


Partial state block ack operation Onreceiving aQoSDataframewithsequencenumberSN,therecipient checkstoseeifit has a record of the block ack scoreboard for that block ack session, where the session is identified by the transmit address (TA) and TID. If not, then it creates a scoreboard for that session with WinEnd = SN and WinStart = WinEnd − WinStart + 1, perhaps reusing memory from another session. The correct reception of the data frame is recorded by setting a 1 in the position representing SN, i.e. WinEnd. For each data frame thereafter: * If SNiswithin the current scoreboard window, i.e. WinStart ≤ SN ≤ WinEnd, then the scoreboard records receipt at the offset represented by SN.* If SNis outside the current scoreboard window, but within half sequence space range, i.e. WinEnd < SN < WinStart + 211, then the scoreboard is shifted right to accom modate SN. * If SN is more than half the sequence space beyond the window, i.e. WinStart + 211 < SN <WinStart, then no change is made. The scoreboard operation is illustrated in Figure 9.17. Here a QoS Data frame with sequence number 102 is received and the recipient creates a new scoreboard with a mark for 102 at its rightmost edge. With the next aggregate, QoS Data frames 103 and 105 are received correctly and the scoreboard is shifted right to accommodate the new entries. QoS Data frame 100 falls within the scoreboard sequence number range and is simply markedup.Notethat inthis sequence the QoSData frameshave their AckPolicyfieldset to Block Ack. When the BAR is received, the recipient shifts the scoreboard to the right so that WinStart =SSNfromtheBAR(100inthiscase)andreturnsaBAframewiththecontents of the scoreboard. Note that the sequence numbers shown are for illustration only. In practice, the sequence number is a 16-bit value composed of a 12-bit MSDU number and a 4-bit fragment number. The major difference between partial state and full state block ack operation is the transient nature of the scoreboard retained by the recipient. Under partial state block ack the originator should ensure that it retrieves the ack state with high probability before another station has a chance to send data to the recipient and potentially erase the session’s block ack scoreboard. In practice this means that the originator should attempt to retrieve the block ack scoreboard before the end of each TXOP. If occasionally the block ack scoreboard is not retrieved during the TXOP (perhaps the BA frameis received in error) then there is still a good chance that a subsequent immediate channel access by the same station will occur before data belonging to another block ack session is received by the recipient, erasing the session’s block ack scoreboard. The originator can thus simply use a BAR frame in a subsequent channel access to retrieve the ack state. In the unlikely event that the block ack scoreboard is no longer available, the BAwill show all zeros and the originator will need to retransmit the MSDUs. Alternatively, if the originator sent a single aggregate consisting of QoS Data frame with Normal Ack policy and no BA was received, then the originator may assume that none of the QoS Data frames made it through and simply retransmit the QoS Data frames. While more complex behavior is permitted, most implementations will conform to partial state rules by sending a single aggregate in each TXOP, soliciting a BA by setting the ack policyofthe QoSDataframesmakinguptheaggregatetoNormalAck.Ontherare occasion when an MSDU lifetime expires before it is acknowledged, the originator will send a BARframetoflush the holes in the reorder buffer. An originator implementing this behavior would workwithbothafullstate andpartial state implementation in the recipient.
08-19
【从高压输电线的架空地线中汲取电能】一个25千瓦受控电源从735千伏线路的架空地线中汲取电能的SimPowerSystems模型(Simulink仿真实现)内容概要:本文介绍了一个基于SimPowerSystems的Simulink仿真模型,用于模拟从735千伏高压输电线的架空地线中汲取25千瓦电能的受控电源系统。该模型聚焦于高压输电线路中架空地线的能量回收技术,通过仿真手段实现对电能采集过程的建模与控制策略验证,体现了电力系统中新型能源获取方式的技术可行性与工程应用潜力。文中还提及该资源属于一系列电力系统仿真研究的一部分,涵盖微电网、储能优化、碳流追踪、鲁棒调度等多个前沿方向,配套提供Matlab/Simulink代码及网盘资料链接,便于科研人员复现与拓展研究。; 适合人群:具备电力系统基础知识、熟悉Matlab/Simulink仿真环境,从事电力工程、能源回收或智能电网相关研究的科研人员及研究生;有一定编程与建模仿真经验的高年级本科生或工程技术人员。; 使用场景及目标:①研究高压输电线路中架空地线的能量回收机制与建模方法;②掌握基于Simulink的电力系统仿真技术,特别是受控电源与电网交互的动态特性分析;③为开展能源 harvesting、分布式供能、电力电子变换器控制等相关课题提供参考模型与技术支撑; 阅读建议:建议结合提供的仿真模型文件进行实操演练,重点理解系统结构设计、参数设置与控制逻辑实现;同时可延伸学习文档中提到的其他电力系统优化与仿真案例,以拓宽研究视野和技术积累。
线性反馈移位寄存器(LFSR)是一种在数字电路和密码学中常用的移位寄存器。下面是一个实现 `LFSR` 类的 Python 代码,包含 `__init__`、`bit`、`step`、`__str__` 方法及主函数调用: ```python class LFSR: def __init__(self, seed, taps): """ 初始化 LFSR 类 :param seed: 初始状态 :param taps: 抽头位置 """ self.state = list(map(int, str(seed))) self.taps = taps def bit(self, index): """ 获取指定索引位的值 :param index: 索引位置 :return: 指定索引位的值 """ return self.state[index] def step(self): """ 执行一次 LFSR 迭代并返回新的最右位 :return: 新的最右位 """ feedback = 0 for tap in self.taps: feedback ^= self.state[tap] new_bit = feedback self.state = [new_bit] + self.state[:-1] return new_bit def __str__(self): """ 返回 LFSR 的字符串表示 :return: LFSR 的字符串表示 """ return ''.join(map(str, self.state)) if __name__ == "__main__": # 初始化 LFSR seed = 1011 taps = [0, 2] lfsr = LFSR(seed, taps) # 输出初始状态 print("初始状态:", lfsr) # 获取指定索引位的值 index = 1 print(f"索引 {index} 位的值:", lfsr.bit(index)) # 执行一次 LFSR 迭代 new_bit = lfsr.step() print("执行一次迭代后的新最右位:", new_bit) print("执行一次迭代后的状态:", lfsr) ``` ### 代码解释 1. **`__init__` 方法**:用于初始化 LFSR 的初始状态和抽头位置。 2. **`bit` 方法**:用于获取指定索引位的值。 3. **`step` 方法**:执行一次 LFSR 迭代,计算反馈位并更新状态,返回新的最右位。 4. **`__str__` 方法**:返回 LFSR 的字符串表示。 5. **主函数**:初始化 LFSR,输出初始状态,获取指定索引位的值,执行一次迭代并输出结果。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值