We hope that you are doing great and enjoying your coding journey so far. It is really important that you always stay excited to learn something new.
So in an urge to stay young let's start with something fresh. In this article we will introduce you to a new topic, "Bit Manipulation". We will discuss couple of things around this topic such as:
- Types and Range
Let's discuss each of them in detail.
- Suppose you initiated any variable, say x of type int with 57; this number is of decimal format but as you might know that everything is stored in binary format inside the machine.
- So the value that corresponds to 57 in binary format is stored instead of 57 inside the memory stack.
- But if you print x then this binary form is converted into decimal form again. And you will get 57 printed and not its binary form.
- To store numbers, we have 4 types; Byte, Short, Int and Long.
- Byte can store 8 bits, Short can store 16 bits, Int can store 32 bits and Long can store 64 bits.
- Since each bit has two options to store, that is it can either store 1 or 0.
- This implies Byte can represent 2^8 distinct numbers, Short - 2^16, Int - 2^32 and Long - 2^64.
- But how do these store negative and positive numbers? Let's see.
- Let's assume that a new type Nibble has been introduced in Java. And it can store 4 bits which implies it is capable of representing 2^4 i.e. 16 distinct numbers.