This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes characters in a byte Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "characters in a byte" Watch "characters in a byte" New topic
Author

characters in a byte

Muhammad Imad Qureshi
Ranch Hand

Joined: Sep 13, 2005
Posts: 238
Someone asked me following questions and I didn't know the answer

If you have to store a ticker, how many characters can you store in a 4 byte integer.

I thought that each byte will have 8 bits so the answer would be 32. But apparently that is not correct. She told me the correct answer is 2 raise to the power of 32 and then divide by 26 (for 26 alpha characters).

Can someone please explain?

Thanks
Imad
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9946
    
    6

a four-byte integer does have 32 bits - and each one can be 'on' or 'off'. That gives you 2^32 possible combinations of bits.

But I'm not sure how she got the "divide by 26" part. That doesn't make sense to me.


Never ascribe to malice that which can be adequately explained by stupidity.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32651
    
    4
Surely it would be (int) (32 * log2 / log 26)? But that would be awkward to implement: easiest done by using base 26 numbers.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12267
    
    1
Besides, a ticker will need 26 alpha and 10 numeric characters.

Or you could use Baudot which uses only 5 bits so you could get 6 characters in 30bits

Bill

Java Resources at www.wbrogden.com
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16687
    
  19

Muhammad Imad Qureshi wrote:Someone asked me following questions and I didn't know the answer

If you have to store a ticker, how many characters can you store in a 4 byte integer.

I thought that each byte will have 8 bits so the answer would be 32. But apparently that is not correct. She told me the correct answer is 2 raise to the power of 32 and then divide by 26 (for 26 alpha characters).

Can someone please explain?

Thanks
Imad


Working out the math... "2 raise to the power of 32 and then divide by 26 (for 26 alpha characters)" --> (2 ^ 32) / 26 --> 165,191,049 characters.

So, basically, a 4 byte integer, should hold enough information to fill an encyclopedia. Hmmm... there is definitely something wrong here...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: characters in a byte
 
Similar Threads
help with simple IO code ?
short-circuit operators
Another Question?
reading foreign characters (say, multi-byte charcters, Japanese, Turkish, etc) from a file in java.
Understanding Byte Data and Character Encoding