BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #46150同步于 2010/11/12
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

【求助】如何快速找出哪一位被置1

arm
2010/11/12镜像同步18 回复
一个64bit的数据,只可能有一位(bit)被置1,如何能快速找到这一位? 例:输入0x8000 0000 0000 0000,输出64 ps:移位的方法就不用说了,有更快的方法吗,最好4步内完成。 谢!
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
wks机器人#1 · 2010/11/12
__builtin_ctz 这个怎么样?
renne机器人#2 · 2010/11/12
用bitset吧…… 【 在 arm (armstrong) 的大作中提到: 】 : 一个64bit的数据,只可能有一位(bit)被置1,如何能快速找到这一位? : 例:输入0x8000 0000 0000 0000,输出64 : ps:移位的方法就不用说了,有更快的方法吗,最好4步内完成。 : ...................
wks机器人#3 · 2010/11/12
参考: http://www.topcoder.com/tc?module=Static&d1=tutorials&d2=bitManipulation
arm机器人#4 · 2010/11/12
【 在 renne 的大作中提到: 】 : 用bitset吧…… : 【 在 arm (armstrong) 的大作中提到: 】 : : 一个64bit的数据,只可能有一位(bit)被置1,如何能快速找到这一位? : ................... 能说具体点儿吗 不懂
arm机器人#5 · 2010/11/12
【 在 wks 的大作中提到: 】 : __builtin_ctz 这个怎么样? : -- : 免费贴代码:http://wksprivate.appspot.com/pastebin/ : ................... 这个能在4步内完成吗 不了解这个
zzcc机器人#6 · 2010/11/12
【 在 arm 的大作中提到: 】 : 这个能在4步内完成吗 不了解这个 一步就完成了。。。 64 - __builtin_clz(x)
ki机器人#7 · 2010/11/12
方法1 1 /** 2 * compat.c - Tweaks for Windows compatibility 3 * 4 * Copyright (c) 2002 Richard Russon 5 * Copyright (c) 2002-2004 Anton Altaparmakov 6 * 7 * This program/include file is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License as published 9 * by the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program/include file is distributed in the hope that it will be 13 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program (in the main directory of the Linux-NTFS 19 * distribution in the file COPYING); if not, write to the Free Software 20 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 */ 22 23 #ifdef WINDOWS 24 25 #ifdef HAVE_CONFIG_H 26 #include "config.h" 27 #endif 28 29 #include "compat.h" 30 31 /* TODO: Add check for FFS in the configure script... (AIA) */ 32 33 #ifndef HAVE_FFS 34 /** 35 * ffs - Find the first set bit in an int 36 * @x: 37 * 38 * Description... 39 * 40 * Returns: 41 */ 42 int ffs(int x) 43 { 44 int r = 1; 45 46 if (!x) 47 return 0; 48 if (!(x & 0xffff)) { 49 x >>= 16; 50 r += 16; 51 } 52 if (!(x & 0xff)) { 53 x >>= 8; 54 r += 8; 55 } 56 if (!(x & 0xf)) { 57 x >>= 4; 58 r += 4; 59 } 60 if (!(x & 3)) { 61 x >>= 2; 62 r += 2; 63 } 64 if (!(x & 1)) { 65 x >>= 1; 66 r += 1; 67 } 68 return r; 69 } 70 #endif /* HAVE_FFS */ 71 72 #endif /* WINDOWS */ 73 74
ki机器人#8 · 2010/11/12
方法2 1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 1992 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 ! .seg "data" 28 ! .asciz ident "%Z%%M% %I% %E% SMI" 29 .seg ".text" 30 31 .file "ffs.s" 32 33 #include <sun4/asm_linkage.h> 34 35 ENTRY(ffs) 36 tst %o0 ! if zero, done 37 bz 2f 38 clr %o1 ! delay slot, return zero if no bit set 39 1: 40 inc %o1 ! bit that will get checked 41 btst 1, %o0 42 be 1b ! if bit is zero, keep checking 43 srl %o0, 1, %o0 ! shift input right until we hit a 1 bit 44 2: 45 retl 46 mov %o1, %o0 ! return value is in o1 47 SET_SIZE(ffs) 48
QRush机器人#9 · 2010/11/13
这个貌似只适用32bit的数据,如果是64bit数据的话,应该还得在47行和48行之间多增加一个if语句吧。 【 在 ki 的大作中提到: 】 : 方法1 : 1 /** : 2 * compat.c - Tweaks for Windows compatibility : ...................