473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

c# Retrieving INDEX of a bit rather then its VALUE

I would like to retrieve the INDEX of a bit rather then its VALUE while scanning for MSB or LSB

Example from http://aggregate.org/MAGIC/#Most%20Significant%201%20Bit:
unsigned int msb32(register unsigned int x)
{
x |= (x >> 1);
x |= (x >> 2);
x |= (x >> 4);
x |= (x >> 8);
x |= (x >> 16);
return(x & ~(x >> 1));
}

will not provide the index of a bit (range 0-32) but values from 0 - Maxvalue .

How can i achieve that?
Jan 7 '08 #1
1 1273
Plater
7,872 Expert 4TB
if i read that correctly that function will find the most significant bit that is a "1" and then set all less-significant bits to "1", regardless of what they were prior.
Which if you process that, could tell you which bit was the highest bit with a "1".

I don't see what that has to do with bitwise endian though. As if an 8bit value for the decimal number 10 was:
stored msb->lsb:
0000 1010
or lsb->msb
0101 0000

Regardless of which way it is stored, using the << and >> will have the same result because they too will be setup for that bit order.


Byte-wise endian on the other hand can be easy to check for. Tips from Fortran:
If you have an Int16 and the value is set to decimal 10, then casting it to an 8bit byte, will either give you a 0 or a 10, depending on the order.
If you can cast it to a byte[], you can do it for int32, int64 values as well.

If you really just want the index of the most significant 1s bit:
Expand|Select|Wrap|Line Numbers
  1. public static int msb(UInt16 val)
  2.         {
  3.             //16bit val
  4.             //Range:  0 to ((2^16)-1)
  5.             if ((val & 0x8000) > 0)
  6.                 return 15;
  7.             if ((val & 0x4000)>0)
  8.                 return 14;
  9.             if ((val & 0x2000)>0)
  10.                 return 13;
  11.             if ((val & 0x1000)>0)
  12.                 return 12;
  13.  
  14.             if ((val & 0x0800)>0)
  15.                 return 11;
  16.             if ((val & 0x0400)>0)
  17.                 return 10;
  18.             if ((val & 0x0200)>0)
  19.                 return 9;
  20.             if ((val & 0x0100)>0)
  21.                 return 8;
  22.  
  23.             if ((val & 0x0080)>0)
  24.                 return 7;
  25.             if ((val & 0x0040)>0)
  26.                 return 6;
  27.             if ((val & 0x0020)>0)
  28.                 return 5;
  29.             if ((val & 0x0010)>0)
  30.                 return 4;
  31.             if ((val & 0x0008) > 0)
  32.                 return 3;
  33.             if ((val & 0x0004) > 0)
  34.                 return 2;
  35.             if ((val & 0x0002) > 0)
  36.                 return 1;
  37.             if ((val & 0x0001) > 0)
  38.                 return 0;
  39.             return -1;
  40.         }
  41.  
I have folded this into a simple for loop before, but could not find the code offhand and didn't want to think about it counting down (i think the magic number is like 17 or something?)
Jan 8 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Rogue9 | last post by:
Hi, I´m trying to retrieve an item from a list using a reference called ´draw´' (which is an integer from 1 to 792 in a while loop) using:- draw = 1 while 1: if draw == 792: break b1 =...
3
by: CES | last post by:
All, Is their a way of iterating thru each tag within a form and returning the value given in the id property, by that I mean the below html would return the values idBoxOne, idBoxTwo,...
8
by: asd | last post by:
I need to find the value/index of the previously selected item of a select list. That is, when the user selects an item from the list and a certain condition elsewhere in the form is not met, I...
14
by: Rich | last post by:
Yes, I need to store some values in an array type collection object that can hold 3 or more parameters per index. I have looked at the collection object, hashtable object and would prefer not to...
5
by: Dylan Parry | last post by:
Hi, At the moment I use code like the following: string myString = this.dataReader.IsDBNull(2) ? null : this.dataReader.GetString(2); With a record from the DB that looks like: ...
10
by: Girish | last post by:
Hi Everyone, I am passing a form to a php script for further processing. I am able to retrieve the last value set for that given form variable using $variable=$_REQUEST;
1
by: Olaf Rabbachin | last post by:
Hi folks, I'm looking for a way of retrieving the default-value of a table's field within an Access 2003 MDB. Any hints on how to accomplish this from ASP.Net? Thanks, Olaf -- My .02:...
3
by: BlackShadow33p1 | last post by:
I'm trying to write a program in C++ that gets the handles of all the visible entries in the windows taskbar. The method I've used so far is to send the TB_GETBUTTON message to the taskbar. ...
0
by: Phijo | last post by:
Hello, I'm a Java developer but brand new to ASP (i have done some of the tutorials) so I have some ASP programming background however I cannot find the cause or the fix to this problem. I am...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.