[SOLVED] How to calculate the number of *necessary* bits in a byte ?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to calculate the number of *necessary* bits in a byte ?
E.g. 98 is represented as 1100010 (number of valid bits is 7)
What is the formula to calculate the number of valid bits in a byte ?
Kindly Guide..
P.S.
This question is not related to shell scripts.
and
I did search Google but what I found was solutions regarding problems like number of ON bits in an unsigned int.. etc..
Last edited by Aquarius_Girl; 06-11-2010 at 02:50 AM.
Reason: Formatting
98 is represented as 1100010 (number of valid bits is 7)
Represented where? What do you mean by "valid"? I believe you should start looking for the definitions/meanings of terms such as "bit", "byte", "valid". Your recent threads lead me to believe there are some fundamental gaps in your programming "knowledge".
Not sure what you mean by 'valid bits'... and the 'number of bits in a byte' is always 8.
Your example, however, suggests that you want to find out the number of "significant" bits, i.e. discarding all leading zeros. There's been a method to do that in Java's standard library since version 1.5.0; see listing below. This technique can be generalised to arbitrary-length strings of bits, for example, see the method BitSet.length() in the Java standard library.
Code:
/**
* Returns an <tt>int</tt> value with at most a single one-bit, in the
* position of the highest-order ("leftmost") one-bit in the specified
* <tt>int</tt> value. Returns zero if the specified value has no
* one-bits in its two's complement binary representation, that is, if it
* is equal to zero.
*
* @return an <tt>int</tt> value with a single one-bit, in the position
* of the highest-order one-bit in the specified value, or zero if
* the specified value is itself equal to zero.
* @since 1.5
*/
public static int highestOneBit(int i) {
// HD, Figure 3-1
i |= (i >> 1);
i |= (i >> 2);
i |= (i >> 4);
i |= (i >> 8);
i |= (i >> 16);
return i - (i >>> 1);
}
See my previous reply, which should give you the pointers to get started in working this out. There's no quick formula to find the position of the left-most one (which is effectively what you're asking for), you always have to do some recursive calculation.
The Java method I posted achieves this in just 5 steps for any integer, where the "naive" version of checking each of the 32 bits in turn could take up to 32 steps, so it shows how a little bit of cleverness can help.
Well, having said that, if you have a suitable library function for computing log2 (logarithm to the base of 2), then given a (positive) integer x the left-most one is at position floor(log2(x)). However, the specialised routine used in Integer.highestOneBit() is likely to be significantly faster than computing a general-purpose logarithm and then rounding.
Represented where? What do you mean by "valid"? I believe you should start looking for the definitions/meanings of terms such as "bit", "byte", "valid". Your recent threads lead me to believe there are some fundamental gaps in your programming "knowledge".
Or perhaps just some gaps associated with language/semantics....Let's give the benefit of doubt--OK?
Anisha;
Depending on what you are trying to do, it might be better to ask how many bits are **necessary**. For example, suppose you simply want to know if your number is 98 or zero. In this case, you only need ONE bit: If it is "1", then you have 98---if it is "0", you have zero. The point is that the number of bits required is set by the number of different states that need to be represented. (This is commonly called "entropy".)
As already stated, the term "byte" is simply another unit and is--by definition--8 bits. (just as a foot is 12 inches, and a pound is 16 ounces).
You will hear people talk about "information content"--especially in the context of compression schemes. For a photograph, one can calculate the actual information content, and hence the number of bits or bytes required to send that photograph throught an information system. For example, start with a picture file in the GIMP RGB format. Then save it as a jpeg and look at the difference in file size.
Returning to your example, the number of valid (or necessary or significant, etc.) bits (or bytes) in the number 98 depends entirely on the context in which that number is being used.
That is the way I convert decimal to binary on PAPER. It didn't strike me somehow that I can use this method again to calculate the number of *necessary* bits in a byte !
In the interests of being precise, you can talk about the number of "valid" or "necessary" bits or bytes to represent or encode a quantity of information, but a byte is still defined as 8 bits.
<<Edit: A Byte has to have 8 bits, but some of them might not be doing anything useful.>>
Keep separate two concepts:
---How much you need of something
---The units and their relationships
I need 64 ounces of milk, but--at the store--I must be prepared to accept 1/2 gallon
See my previous reply, which should give you the pointers to get started in working this out. There's no quick formula to find the position of the left-most one (which is effectively what you're asking for), you always have to do some recursive calculation.
The Java method I posted achieves this in just 5 steps for any integer, where the "naive" version of checking each of the 32 bits in turn could take up to 32 steps, so it shows how a little bit of cleverness can help.
Well, having said that, if you have a suitable library function for computing log2 (logarithm to the base of 2), then given a (positive) integer x the left-most one is at position floor(log2(x)). However, the specialised routine used in Integer.highestOneBit() is likely to be significantly faster than computing a general-purpose logarithm and then rounding.
Many thanks for the effort
I did try to study the method shown by you, but maybe because I am not that sharp, I couldn't clearly grasp it.
But I shall study it again (as I am currently working on bit packing) and then re-post in this thread, if any problems are faced.
For a photograph, one can calculate the actual information content, and hence the number of bits or bytes required to send that photograph throught an information system. For example, start with a picture file in the GIMP RGB format. Then save it as a jpeg and look at the difference in file size.
Returning to your example, the number of valid (or necessary or significant, etc.) bits (or bytes) in the number 98 depends entirely on the context in which that number is being used.
Thanks for the concern,
That photography example is nice but if we talk about bits and you say number of necessary bits depend on the context, then in what context we will be using less number of bits than actual 1100010,
In any case if we reduce or increase the number of bits, the number will be changed ?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.