Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# characters in a byte

Ranch Hand

Joined: Sep 13, 2005
Posts: 238

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).

Thanks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10540

10

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 34638

13
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: 12569

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: 17629

33

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).

Thanks

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

I agree. Here's the link: http://aspose.com/file-tools

subject: characters in a byte