Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to find the most significant bit [duplicate]

A friend of mine was asked at an interview the following question: "Given a binary number, find the most significant bit". I immediately thought of the following solution but am not sure if it is correct.

Namely, divide the string into two parts and convert both parts into decimal. If the left-subarray is 0 in decimal then the do binary search in the right subarray, looking for 1.

That is my other question. Is the most significant bit, the left-most 1 in a binary number? Can you show me an example when a 0 is the most significant bit with an example and explanation.

EDIT:

There seems to be a bit of confusion in the answers below so I am updating the question to make it more precise. The interviewer said "you have a website that you receive data from until the most significant bit indicates to stop transmitting data" How would you go about telling the program to stop the data transfer"

like image 592
user2398832 Avatar asked Jun 10 '13 15:06

user2398832


People also ask

How do you determine the most significant bit?

Given a number, find the greatest number less than the given a number which is the power of two or find the most significant bit number . The most significant bit corresponds to decimal number 8.

How do I know if I have MSB or LSB?

Answer: In a digital data bit string, the MSB is a bit of the highest digit, and the LSB is a bit of the lowest digit. Digital data is binary, and like ordinary numerical notation, the left end is the highest digit, while the right end is the lowest digit.

Which bit has the highest weightage MSB or LSB?

Negative Numbers. In a binary number, the bit furthest to the left is called the most significant bit (msb) and the bit furthest to the right is called the least significant bit (lsb). The MSB gives the sign of the number (sign bit) , 0 for positive and 1 for negative.

What is the MSB?

The term "money services business" includes any person doing business, whether or not on a regular basis or as an organized business concern, in one or more of the following capacities: (1) Currency dealer or exchanger. (2) Check casher. (3) Issuer of traveler's checks, money orders or stored value.


1 Answers

The edited question is really quite different, though not very clear. Who are "you"? The website or the programmer of the program that reads data from the website? If you're the website, you make the program stop by sending a value (but what, a byte, probably?) with its most-significant bit set. Just OR or ADD that bit in. If you're the programmer, you test the most-significant bit of the values you receive, and stop reading when it becomes set. For unsigned bytes, you could do the test like

bool stop = received_byte >= 128;
or
bool stop = received_byte & 128;

For signed bytes, you could use

bool stop = received_byte < 0;
or
bool stop = received_byte & 128;

If you're not reading bytes but, say, 32bit words, the 128 changes to (1 << 31).

like image 98
harold Avatar answered Sep 18 '22 16:09

harold