Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to find the least most significant bit set in a byte

Status
Not open for further replies.

gprashanth911

Newbie level 5
Newbie level 5
Joined
Jul 18, 2006
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,327
Hi,

I am looking for all the possible solutions to find the least most significant bit set in a byte. Provided the algorithm should be O(1). If any one has any sort of info please share.

thanks in advance
 

jhbbunch

Full Member level 4
Full Member level 4
Joined
Feb 21, 2006
Messages
197
Helped
17
Reputation
34
Reaction score
3
Trophy points
1,298
Activity points
2,902
A function like this should work. It's to find the LSB of a byte. It returns aa value 0 to 7 or -1 for no bit set You could do a zero test rigth off the bat if you wanted (if value == 0 return -1). I haven't tested it so be warned.

signed int Find_Byte_LSB(value)
{

int i = 0;

while (i <= 7)
{
if (1 << i && value)
return i;
i++;
}

return -1;

}
 

dsp4us

Member level 2
Member level 2
Joined
Jan 21, 2003
Messages
48
Helped
10
Reputation
20
Reaction score
3
Trophy points
1,288
Activity points
373
First of all there is no such a thing as "the least most significant bit". There is either least or most significant bit (LSB or MSB). Where are they located in the byte is actually depends on "bit endianness" (https://en.wikipedia.org/wiki/Endianness)
Although usually MSB is the left most bit in the byte and LSB - right. Thus to find what "ByteVar" has in MSB or LSB, this code could be used (or some derivative of that):

for MSB
bit = (ByteVar & 0x80) ? 1: 0;
or
bit = (unsigned byte)(ByteVar & 0x80) >> 7;

for LSB
bit = (ByteVar & 0x01) ? 1: 0;
or simple
bit = ByteVar & 0x01;



Cheers.
 

jayavardhan

Full Member level 2
Full Member level 2
Joined
Sep 1, 2006
Messages
124
Helped
3
Reputation
6
Reaction score
0
Trophy points
1,296
Location
India
Activity points
1,840
AND the value with 0x01 if the result is 1 last bit is set else not set.
 

rocking1234

Member level 1
Member level 1
Joined
May 2, 2009
Messages
36
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Location
India
Activity points
1,535
Just 3 step away

Let say no = x

1: substract the number by 1 => x-1
2: invert the result => ~(x-1)
3: And the result with the orginal number => X && [~(x-1)]

Then right shift he result untill u get the set bit ....deal done...:D
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top