一个32位的整数,如何取得整数中每一位?

chenkangli 2004-08-15 09:57:32
一个32位的整数,如何取得整数中每一位?
...全文
304 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
benniao2 2004-08-16
  • 打赏
  • 举报
回复
dim i as integer
'最后一位是
dim last as integer = i-i/10*10
wangh2122 2004-08-15
  • 打赏
  • 举报
回复
Function GetBit(ByVal SourceNumber As Long, ByVal BitNo As Byte) As Byte
If SourceNumber And (2 ^ BitNo) Then GetBit = 1
End Function

SourceNumber:32位长整数
BitNo:从右算起第几位,0-31
返回值0或1

这样应该是最快的了
Geo_Cra 2004-08-15
  • 打赏
  • 举报
回复
Function GetBit(ByVal SourceNumber As Long, ByVal BitNo As Byte) As Byte
GetBit = (SourceNumber And (2 ^ (BitNo - 1))) \ (2 ^ (BitNo - 1))
End Function
说明:
SourceNumber:原32位长整数
BitNo:从右算起第几位
返回值为长整数从右开始某一位的值

VB测试通过,用And运算符要快,可惜没有移位运算,只有用整除来模拟
熊孩子开学喽 2004-08-15
  • 打赏
  • 举报
回复
32位整数吧,简单,现场写个函数给你:
Public Function GetBit(Byval SourceNumber as long , Byval BitNo as byte ) as byte
for BitNo =BitNo to BitNo-1 step -1
SourceNumber =SourceNumber \2
GetBit= SourceNumber Mod 2
next
End Function
说明:
SourceNumber :原32位长整数
BitNo :从右算起第几位
返回值为长整数从右开始某一位的值

xueleic 2004-08-15
  • 打赏
  • 举报
回复
比如
print mid(cstr(12345678901234567890123456789012),20,1)
返回
1
首个字符算第0个
xueleic 2004-08-15
  • 打赏
  • 举报
回复
用cstr转换成字符形,再用mid函数返回指定位置的一个字符
thirdapple 2004-08-15
  • 打赏
  • 举报
回复
And运算符

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧