Monday, August 10, 2015

Research about Java Bytes

Research Findings:
1. byte is a signed type in Java.
2. 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.
3. 10. Assuming that we are going to represent the concept of "raised to the power of" with the "^" symbol (so "10 squared" is written as "10^2"),
4. Our base-10 number system likely grew up because we have 10 fingers, but if we happened to evolve to have eight fingers instead, we would probably have a base-8 number system. You can have base-anything number systems. In fact, there are lots of good reasons to use different bases in different situations.
5. Computers happen to operate using the base-2 number system, also known as the binary number system (just like the base-10 number system is known as the decimal number system).
6. base-2 computers are relatively cheap.
7. So computers use binary numbers, and therefore use binary digits in place of decimal digits.
8. The word bit is a shortening of the words "Binary digIT."
9. Whereas decimal digits have 10 possible values ranging from 0 to 9, bits have only two possible values: 0 and 1. Therefore, a binary number is composed of only 0s and 1s, like this: 1011.
10. You can see that in binary numbers, each bit holds the value of increasing powers of 2. That makes counting in binary pretty easy. Starting at zero and going through 20, counting in decimal and binary looks like this:
11. 0 and 1 are the same for decimal and binary number systems.
12. Bits are rarely seen alone in computers. They are almost always bundled together into 8-bit collections, and these collections are called bytes.
14. Why are there 8 bits in a byte? A similar question is, "Why are there 12 eggs in a dozen?" The 8-bit byte is something that people settled on through trial and error over the past 50 years.
15. With 8 bits in a byte, you can represent 256 values ranging from 0 to 255
16. Bytes are frequently used to hold individual characters in a text document.
17. In the ASCII character set, each binary value between 0 and 127 is given a specific character.
18. Most computers extend the ASCII character set to use the full range of 256 characters available in a byte. The upper 128 characters handle special things like accented characters from common foreign languages.
19. You can see the 127 standard ASCII codes below. Computers store text documents, both on disk and in memory, using these codes
20. Open up a new file in Notepad and insert the sentence, "Four score and seven years ago" in it. Save the file to disk under the name getty.txt. Then use the explorer and look at the size of the file. You will find that the file has a size of 30 bytes on disk: 1 byte for each character.
21. Each character consumes a byte
22. By looking in the ASCII table, you can see a one-to-one correspondence between each character and the ASCII code used.
23. In mathematics and computing, hexadecimal (also base 16, or hex) is a positional numeral system with a radix, or base, of 16.
24. Several different notations are used to represent hexadecimal constants in computing languages; the prefix "0x" is widespread due to its use in Unix and C (and related operating systems and languages).
25. Each hexadecimal digit represents four binary digits (bits)
26. One hexadecimal digit represents a nibble, which is half of an octet or byte (8 bits).
27. In URIs (including URLs), character codes are written as hexadecimal pairs prefixed with %: http://www.example.com/name%20with%20spaces where %20 is the space (blank) character (code value 20 in hex, 32 in decimal).
28. Unicode is a computing industry standard for the consistent encoding, representation, and handling of text expressed in most of the world's writing systems.
29. The key is that the hex literal 0xFF is still an int..that is, it takes up 32 bits. That makes it look like this in binary:
00000000 00000000 00000000 11111111
30. The unary bitwise complement operator "~" inverts a bit pattern;
31. Firstly, you can not shift a byte in java, you can only shift an int or a long. So the byte will undergo promotion first, e.g.
32. The & operator is a bitwise "And". The result is the bits that are turned on in both numbers. 1001 & 1100 = 1000, since only the first bit is turned on in both.
33. This MessageDigest class provides applications the functionality of a message digest algorithm, such as SHA-1 or SHA-256. Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value.
34.  The digest method can be called once for a given number of updates. After digest has been called, the MessageDigest object is reset to its initialized state.
35.  


Now, x >> N means (if you view it as a string of binary digits):
The rightmost N bits are discarded
The leftmost bit is replicated as many times as necessary to pad the result to the original size (32 or 64 bits), e.g.
00000000000000000000000000101011 >> 2 -> 00000000000000000000000000001010

11111111111111111111111111010100 >> 2 -> 11111111111111111111111111110101

Keywords:
Bit, Hexadecimal digits(4-bits), Byte(8-bits), ASCII, Unicode,

Examples:
(1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (1 * 2^0) = 8 + 0 + 2 + 1 = 11

References:
http://www.asciitable.com/
http://unicode-table.com/en/#0004
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
http://stackoverflow.com/questions/3312853/how-does-bitshifting-work-in-java
















No comments:

Post a Comment