tags:

views:

179

answers:

5

In Sun' tutorial it says about a byte:

byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.

how does it save memory? What is 2's compliment?

+1  A: 

How can you be asking this question with a 4700 rep rating on SO? The answer to what 2's complement is can easily be found on Wikipedia! With regards to how it can save memory, just think about it :

If I don't need to model numbers less than -127 or greater than 128 in my program, I can use an 8-bit construct instead of a 32-bit or 64-bit construct.

Amir Afghani
He got his rep rating by asking questions like this one.
Nathan Hughes
That's bologna - I'm sorry. I can sit here all day and ask questions that I can easily find the answers to on Wiki or elsewhere, but I don't think that means I should or that its helpful to me or the community. I mean, really, 2's complement??
Amir Afghani
@Amir: Nathan's not saying that "mrblah" **deserves** his rep, just how he got it. Users who ask 580 questions and answer not a single one deserve a timeout.
Michael Petrotta
I didn't mean to challenge "mrblah's" integrity or put him on the spot. I just think that sometimes exhausting all reasonable answers or making a best effort on one's own before raising your hand is a good practice. I'll be more constructive with future responses.
Amir Afghani
One of his questions: "What is a java bean" http://stackoverflow.com/questions/1973073/what-is-a-java-bean
Chad Okere
+4  A: 

It saves memory by consuming only eight bits of storage, versus 32 for integers. The size of arrays is directly proportional to the size of the contained datatype; an array of integers will consume about four times more memory (handwaves) than an array of bytes.

From Wikipedia:

A two's-complement system or two's-complement arithmetic is a system in which negative numbers are represented by the two's complement of the absolute value;1 this system is the most common method of representing signed integers on computers.[2] In such a system, a number is negated (converted from positive to negative or vice versa) by computing its two's complement. An N-bit two's-complement numeral system can represent every integer in the range −2^(N−1) to +2^(N−1)−1.

Michael Petrotta
A: 

A single byte in a Java program probably takes up 4 bytes of space anyway, for technical reasons. But when you have an array of bytes, each element in the array really only takes up one byte, while integers (for example) take up 4 bytes each - for large arrays of bytes, you save almost 75% of the space.

As for 2's complement (with an E in the middle!): Check out Wikipedia http://en.wikipedia.org/wiki/Two%27s_complement !

Carl Smotricz
A: 

Bytes save memory due to being only one byte long, whereas most other data types commonly used are 4 or 8 bytes long.

Twos complement is the almost universal way to encode signed numbers as binary. This encoding has the nice property that incrementing any value as if it were just binary, gives you the next integer value, even as the value passes through zero. The same CPU circuitry can compute signed or unsigned integers.

DarenW
A: 

The other thing is that, mostly for historical reasons, most data is broken up into 8-bit bytes. It could have been any number, but 8-bit computers were really popular when things were really first getting standardized, I guess.

So for example, text is often stored with one 8-bit byte per letter (in ASCII mode). Data files are often indexed using pointers to byte indexes. People talk about kilobytes, and megabytes, and they mean 1024*8 bits. or 220 * 8 bits.

Bytes are kind of the universal unit of computing for a lot of purposes. If you want to edit a standard file read by other programs, you're most likely going to need to load it into a byte[] and manipulate individual bytes at some point.

If sun didn't include a byte datatype, writing java programs that worked with data or text from other programs would have been a huge pain. You would have to load integers, and do shift and and operations to isolate individual bits, and divide indexes by 4 all the time. Not fun.

So bytes weren't really added to save memory, but for compatibility sake.

Because a byte can have one of 28 = 256 possible values, Sun decided they should denote -128 through 127, rather then 0 through 255, because they didn't want to deal with having signed/unsigned numbers (none of their datatypes are signed)

They used two's complement because it's the standard way of dealing with negative numbers.

Chad Okere