Showing posts with label data type. Show all posts
Showing posts with label data type. Show all posts

Friday, February 14, 2014

Can you store all of the possible (Java) byte numbers on your laptop memory?

Q- Can you store all of the possible (Java) byte numbers on your laptop memory?
A- Probably yes. To build the answer, let's follow these steps:

1- How much memory 1 byte uses in Java?
8 bits (= 2^3 bits).
        Please check this post and this one on a new window.

2- How many possible representations of numbers in byte are available in Java?
256 (=2 ^ 8) possibilities, which can vary between -2^7 and +2^7 -1.
        Please check the reference on a new window.

3- How much memory is needed to store all the possible representations of numbers in byte?
2 K bits or 256 bytes  (256 x 8 bits = 2^8 * 2^3 = 2^[8+3] = 2^ 11 bits).
      
4- How much memory is available on your laptop?
I don't know, but you probably have enough!



http://interviewqa4java.blogspot.com/2014/02/how-many-bytes-are-in-1-gb.html

How many bytes are in 1 GB?

Q) How many bytes are in 1 GB?
We could approximate that 1 Gigabyte has 1 billion bytes, but the correct answer is  2^30 bytes.

How can I remember this answer in the future? Well here is one trick to always remember this.  Please read how to build the following table:
  1. Starting with 1 byte = 2^0 bytes, make a table multiplying the previous row by 2, or just annotate the next power of 2:   2^0, 2^1, 2^2, 2^3, etc.
  2. Once you reach 2^10 bytes, you can call it  1 KB (which is not 1,000, but 1024 bytes
  3. For 2^10 to 2^19, the table mirrors the first 10 rows, except for we add the unit K (Kilo). So 2^10 B is 1 KB, 2^11 B is 2 KB, 2^12 B is 4 KB, and so forth.
  4. Once you reach 2^20 bytes, you can call it  1 MB (which is not precisely 1million bytes, but 1024^2 bytes)
  5. The rule #3 is analog for 2^20 to 2^292^30 to 2^39, etc. So 2^25 B [= 2^(20+ 5) B] is 2^5 MB or  32 MB and 2^37 [= 2^(30+ 7) B] is 2^7 MB 128 GB.



Decimal
(Bytes)
Digital Storage (Bytes)
2^0
1
2^1
2
2^2
4
2^3
8
2^4
16
2^5
32
2^6
64
2^7
128
2^8
256
2^9
512
2^10
1 K (1024)
2^11
2 K
2^12
4 K
...
...
2^20
1 M (1024^2)
2^21
2 M
...
...
2^30
1 G (1024^3)
2^40
1 T

How many bits are in 1 byte?

Q-01) How many bits are in 1 byte?
In most of the cases, 1 byte refers to 8 bits.

Q-10) - How many possible numbers can be represented with 1 bit? 
Each 1 bit can hold 2 (= 2^1) possible values: 0 or 1.

Q-11) How many possible numbers can be represented with 1 byte? 
Each 1 byte (=8 bits) can hold 256 (= 2^8) possible values: 0, 1, 10, 11, 100, 101, 110, 111, 1000, ... , 11111111 .



Binary
Decimal
00000000
0
00000001
1
00000010
2
00000011
3
00000100
4
...
...
11111101
253
11111110
254
11111111
255

Thursday, March 29, 2012

How many bits are used to store a long variable, in Java?

How many bits are used to store a long variable, in Java?
64-bits.

Here are the primitive data types in Java and their sizes:
  • byte       8-bits
  • short    16-bits
  • int        32-bits
  • long     64-bits
  • float     32-bits
  • double 64-bits
  • char     16-bits
  • boolean  unknown (I know it seems weird why the answer is not 1-bit. If you know the reason, please add a comment!)
 
 For more details, check the reference.