C#如何获取位索引 [英] C# how to get the index of bit

查看:132
本文介绍了C#如何获取位索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想得到第一位的索引等于1,以及最后一位等于1的索引。例如:



data = 0x3E



第一位= 1



最后一位= 5



我该怎么办?

I want to get the index of the first bit that equal to 1, and the index of the last bit that equal to 1. For example:

data=0x3E

first bit = 1

last bit = 5

How can I do this?

推荐答案

伪代码:
function get_lowest_bit(in input, out bit_pos) returns true if input is not 0, false otherwise
  for bit_pos = 0 to max_bits-1
    if (input bit_and (1 left_shift bit_pos) not_equal 0
       return true
    end if
  end for
  return false
end function





同样适用于高位。

干杯

Andi

PS:我认为这是一个家庭作业,这就是为什么我不给你C#解决方案并留下最高位的练习为你; - )



Likewise for the upper bit.
Cheers
Andi
PS: I assume this is a homework, that's why I do not give you the C# solution and leave the exercise for the highest bit for you ;-)


这篇关于C#如何获取位索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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