LinuxQuestions.org Number of bits (field in general) required to store decimal number
 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.

11-01-2014, 02:00 PM   #1
srinietrx
Member

Registered: May 2013
Posts: 101

Rep:
Number of bits (field in general) required to store decimal number

I am trying to write program as
if I give any decimal number as input n, it should give output
x = log2n i.e
log n/log2

Quote:
 For example n = 35; x = log 35/log2 = 5.12 rounding off next number gives 6. 6 fields are required.
I can write program in this way.But if n value increases, it may lead to wrong result.

So Is there is any better way or algorithm to implement in program.

Last edited by srinietrx; 11-01-2014 at 02:02 PM.

 11-01-2014, 02:11 PM #2 NevemTeve Senior Member   Registered: Oct 2011 Location: Budapest Distribution: Debian/GNU/Linux, AIX Posts: 4,916 Blog Entries: 1 Rep: Code: `int bits= (int) floor (ln (num) / ln (2));` Edit: it's wrong, see below Last edited by NevemTeve; 11-02-2014 at 01:40 AM.
 11-01-2014, 02:29 PM #3 srinietrx Member   Registered: May 2013 Posts: 101 Original Poster Rep: yes. Thank you for introducing new library function floor().But in my case I need to use ceil() as to round up value. Actually I thought that this formula(log n/log 2) fails after certain number. But I will stick with this.
11-01-2014, 10:57 PM   #4
ntubski
Senior Member

Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,793

Rep:
Code:
`int bits= (int) floor (ln (num) / ln (2)) + 1;`
No?

Quote:
 Originally Posted by srinietrx Actually I thought that this formula(log n/log 2) fails after certain number. But I will stick with this.
It fails for 0.

 11-02-2014, 01:39 AM #5 NevemTeve Senior Member   Registered: Oct 2011 Location: Budapest Distribution: Debian/GNU/Linux, AIX Posts: 4,916 Blog Entries: 1 Rep: You are right, it is ceil what you need, my bad. Code: `int bits= (int) ceil (ln (num) / ln (2));` Also you can calculate discrete logarithm: Code: `for (ntmp= num, ndigits= 0; ntmp>0; ntmp /= 2) ++ndigits;` Last edited by NevemTeve; 11-02-2014 at 01:54 AM.
11-02-2014, 08:31 AM   #6
ntubski
Senior Member

Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,793

Rep:
Quote:
 Originally Posted by NevemTeve Code: `int bits= (int) ceil (ln (num) / ln (2));`
I look forward to seeing you encode the number "1" into 0 bits (actually, I suppose you could do it if encoding 0 is not needed).

Quote:
 Also you can calculate discrete logarithm: Code: `for (ntmp= num, ndigits= 0; ntmp>0; ntmp /= 2) ++ndigits;`
This isn't the discrete logarithm (but it is the number bits you'll need).

1 members found this post helpful.
 11-02-2014, 11:35 AM #7 NevemTeve Senior Member   Registered: Oct 2011 Location: Budapest Distribution: Debian/GNU/Linux, AIX Posts: 4,916 Blog Entries: 1 Rep: Thank you for pointing out my faults. Anyways, let's try something to encode positive integers with bitvectors. Well, every positive integer can be written in binary, and the first binary digit is always 'one'. Let's store every bits, except for the first one: Code: ``` 1 - [] 2 - [0] 3 - [1] 4 - [00] 7 - [11] 4 - [00] 8 - [000] 15 - [111]``` PS: Having re-read the Original Post, I've to admit I cannot tell what exactly the question is. Last edited by NevemTeve; 11-02-2014 at 11:37 AM.
 11-04-2014, 07:13 AM #8 sundialsvcs LQ Guru   Registered: Feb 2004 Location: SE Tennessee, USA Distribution: Gentoo, LFS Posts: 10,767 Blog Entries: 4 Rep: A few comments here: (1) There are several well-known floating point number formats. All of them store an exponent and a mantissa. The actual number of "digits" represented internally is reflected in the mantissa, which is then raised to the power of the exponent. Probably the most-common format is the Extended floating-point type of the now-ubiquitous Intel® microprocessors. (2) Each time you see a floating-point number, it has been converted to a printable (decimal) representation ... which, as it happens, is not exactly equal to its float-binary (internal) representation. Thus, "5.12" might be displayed as "5.1" or "5." and in any case it probably is not exactly-equal to "5.1200000...". (3) Determination of "how many digits of the answer are significant" is really a basic engineering question, and would be even if you were using a slide-rule. Functions such as ln() are, after a certain point, just estimations. There's a certain, predictable amount of numeric error, and the subsequent series of operations that you perform will increase (never "decrease") that accumulated error. (4) "Truly-decimal" floating point packages do exist ... which represent the numbers in question as individual digits and perform math exactly the way that you'd do it by hand. But these are typically used for accounting, not engineering applications, and I would not expect any of them to include transcendantal functions.

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post massy Programming 2 03-03-2014 05:43 AM 5883 Programming 3 08-08-2013 06:48 PM drManhattan Programming 10 10-15-2011 08:53 PM turbomen Linux - Newbie 1 11-16-2010 02:48 AM anurupr Programming 8 03-07-2010 10:36 PM

LinuxQuestions.org

All times are GMT -5. The time now is 10:26 PM.

 Contact Us - Advertising Info - Rules - Privacy - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -