LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-11-2010, 02:49 AM   #1
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
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
 
Old 06-11-2010, 03:15 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
How about something simple:
Code:
#!/bin/bash

n=12
x=0

while [[ n -gt 1 ]]
do
	((x++))

	((n /= 2))
done

echo $((x + 1))
 
1 members found this post helpful.
Old 06-11-2010, 03:16 AM   #3
posixculprit
Member
 
Registered: May 2010
Posts: 136

Rep: Reputation: 42
Quote:
Originally Posted by anishakaul View Post
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".
 
Old 06-11-2010, 03:22 AM   #4
ComradeP
LQ Newbie
 
Registered: Oct 2009
Posts: 12

Rep: Reputation: 2
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);
    }
 
Old 06-11-2010, 03:26 AM   #5
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by posixculprit View Post
Represented where?
I meant 98 is read as 1100010 in binary.

Quote:
Originally Posted by posixculprit View Post
What do you mean by "valid"?
By "valid" I meant that though there are 8 bits in a byte, but in the case of number 98 only 7 bits are considered significant.

Quote:
Originally Posted by posixculprit View Post
Your recent threads lead me to believe there are some fundamental gaps in your programming "knowledge".
I never claimed to be an expert programmer,,did I ?
 
Old 06-11-2010, 03:38 AM   #6
ComradeP
LQ Newbie
 
Registered: Oct 2009
Posts: 12

Rep: Reputation: 2
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.
 
1 members found this post helpful.
Old 06-11-2010, 03:43 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by posixculprit View Post
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.
 
1 members found this post helpful.
Old 06-11-2010, 03:50 AM   #8
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by ComradeP View Post
Not sure what you mean by 'valid bits'... and the 'number of bits in a byte' is always 8.
This is not always the case: originally a byte was 4 bits. The wiki page on Bytes has more info. http://en.wikipedia.org/wiki/Byte
 
Old 06-11-2010, 03:53 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by pwc101 View Post
This is not always the case: originally a byte was 4 bits. The wiki page on Bytes has more info. http://en.wikipedia.org/wiki/Byte
Now we're off on a side road----in current practice a byte is 8 bits.
 
Old 06-11-2010, 03:54 AM   #10
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by pixellany View Post
Now we're off on a side road----in current practice a byte is 8 bits.
Yes, that's true. But assumptions are what get you in trouble...
 
Old 06-11-2010, 04:29 AM   #11
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by grail View Post
How about something simple:
Code:
#!/bin/bash

n=12
x=0

while [[ n -gt 1 ]]
do
	((x++))

	((n /= 2))
done

echo $((x + 1))
Thanks for the hint, Grail

I guessed you meant this way:

Let the number be 98

98 / 2 = 49 , Exact division , Carry 0
49 / 2 = 24 , Not Exact division , Carry 1
24 / 2 = 12 , Exact division , Carry 0
12 / 2 = 6 , Exact division , Carry 0
6 / 2 = 3 , Exact division , Carry 0
3 / 2 = 1 , Not Exact division , Carry 1
1 Carry 1

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 !
 
Old 06-11-2010, 05:34 AM   #12
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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

Last edited by pixellany; 06-11-2010 at 05:36 AM.
 
1 members found this post helpful.
Old 06-11-2010, 06:01 AM   #13
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by ComradeP View Post
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.
 
Old 06-11-2010, 06:16 AM   #14
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by pixellany View Post
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 ?
 
Old 06-11-2010, 07:14 AM   #15
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
As I said earlier, if you only need to discriminate between 98 and zero, then you only need ONE bit--- The encoding would be like this:

binary 1 = 98
binary 0 = 0

to discriminate all integer numbers in the range of 0 to 127, you need 7 bits:

2 ** 7 = 128, so 7 bits will encode 128 discrete states


But a BYTE is still 8 bits......
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to copy an entire disk byte-for-byte Pawprint Linux - Software 6 06-16-2011 11:01 AM
How do I calculate the number of days from a particular date using the <cal> utility? deepumnit Linux - Desktop 3 12-31-2007 12:12 PM
Calculate total number of mailq items a1phanumeric Linux - Newbie 1 11-15-2007 11:29 AM
byte to byte remote comparison louisJ Linux - Newbie 3 09-21-2007 05:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration