Skip to main content

Playing with bits...... | PART- 1 |

Hello Code peoples.....

How are you doing ?

Actually, it's a long time after publishing my last blog about important sites of dynamic programming. Now it's time to discuss some crucial thing about bits. Let's begin.

Bits:


What are bits ?? 

Do those things hurt me ? or is it quite difficult to learn the strategy of bit manipulations ?

Opppsssss..!!! I use a couple of boring words like b-i-t m-a-n-i-p-u-l-a-t-i-o-n-s

Don't afraid.

This idea is simple...

You know what , bit are more or less a unit.. You can assume a bit as a unit of words.. Do you know the words of computer ?


It's 0 and 1... Computer is not versatile as human being as it can talk or manipulates its system by using multiple language. In computer, all the numbers and all the other data are stored using 2 based number system or in binary format..

Like "10010100" or "00000000" ..



See ?? 1 and 0's only.

here a single 1 or single  0 will be considered as a bit.


Okey...We have talked a lot. We will go to code section very soon..Before that, just I would like to remind you some operations ....

Bit-wise operations means working with the individual bits other than using the larger or default data types, like integers, floating points, characters, or some other complex types. C/C++ provides a programmer an efficient way to manipulate individual bits of a data with the help of commonly known logical operators like AND(&), OR(|), NOT(~), XOR(^), LEFT SHIFT(<<) and RIGHT SHIFT(>>) operators.

Let's have the fun :

And Operator :


0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1


What is the value of 24 & 4 ?

As computer is not capable of reading those words as decimal number system, it will convert those into binary number system.

Okey, let's see:

24 ->   0001  1000
4   ->   0000  0100
res->   0000  0000

res = 0;
 
See??????  Some of you may think that AND operator is more or less like multiplication
but...it's not..

24 & 4 is 0

If you don't believe me..Run the code in c++:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int a = 24;
    int b = 4;

    cout << (a & b) << endl;

    return 0;
}


(OR) Operator :

0 | 0 = 0
0 | 1 = 1
1 | 0 = 1
1 | 1 = 1
Okey, let's see the same example :
24 ->   0001  1000
4   ->   0000  0100
res->   0001  1100



run the code and comment the output below:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int a = 24;
    int b = 4;

    cout << (a | b) << endl;

    return 0;
}

28...isn't it ??? !!!!!!!!




(XOR) operator

0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0

Now, using the same example above, let's do using ^ (XOR) operator :


24 ->   0001  1000
4   ->   0000  0100
res->   0001  1100

Hey!! is it the same as OR ???
hihi..!!!!!
24 ->   0001  1000

4   ->   0000  0100

res->   0001  1100


X-OR is Exclusive-OR..


It's not the same..


101110111110100 ⇒ 24052
001001101110010 ⇒ 4978
--------------- ^
100111010000110 ⇒ 20102
Do the code :
#include<bits/stdc++.h>
using namespace std;

int main()
{
    int a = 10;
    int b = 4;

    cout << (a ^ b) << endl;

    return 0;
}
See ?? it's not working as OR actually....
(NOT) Operator:
This ~ (NOT) operator, inverts all the bits in a variable. That is, changes all zeros into ones, and changes all ones into zeros.
Remember! that the result of this ~ (NOT) operation highly depends on the length of the bit-string.

Example:
int a = 10;
printf("%d\n", ~a);
the output is -11.
Etai Shuvonkorer faki....
Okey..Let's go to the descriptive part...
0000000000001010 ⇒ a(16 bits)
          ~
1111111111110101 ⇒ -11
Computer stores -11 as 1111111111110101 in binary..
Actaully, 
-11(10) = 11111111111111111111111111110101(2)
Okey..We will talk about bit shifting in the next blog part.. 
InshaAllah;





































Comments

Popular posts from this blog

Remote Dictionary Server

  Today I want to discuss about REDIS Cache. In a simple way, it’s like making web apps more effective. What they say ? From Redis  official website , “Redis is an open-source, in-memory data structure store, used as a database, cache and message broker. It supports data structures . I t can be thought as a NOSql DB which store data as a key value pair in the system memory. Now, let’s talk about caching. Difference Between MongoDB And Redis MongoDB is a disk based database optimized for operational simplicity. It has a huge data volumes. And Redis is an in-memory, persistent  data Structure store that is popular for performing operations with maximum effectiveness. Caching Caching is a process where the data are stored in caches so that those can be retrieved faster. It is for making user experience better. Redis Cache As Redis is an in-memory data structure store  where data access operations are performed faster. That’s why it’s the perfect method for caching. The ...

Some Important learning sites for Dynamic programming

Now, We will discuss some of the most useful sites for learning Dynamic Programming. Here, I have mentioned some of those . :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: medium.freecodecamp.org -  Demystifying Dynamic Programming iarcs.org.in -  Dynamic Programming - Tiling topcoder.com -  Dynamic Programming – From Novice to Advanced illinois.edu -  Dynamic Programming  ;(Exercises are recommended) codechef.com -  Dynamic Programming geeksforgeeks.org -  Dynamic Programming  ;(Contains a lot of practice sessions) MIT OCW (Contains some Advanced topics as well) Dynamic Programming I Dynamic Programming II Dynamic Programming III Dynamic Programming IV Happy Learning...Thanks 😉😉😊😊  

Params in C#

C# HAS A FEATURE. IT CAN SPECIFY A METHOD PARAMETER WHICH WILL TAKE A VARIABLE NUMBER OF  ARGUMENTS. WE USED TO DO THOSE THINGS BY USING POINTER IN C++ C# MAKES OUR LIFE EASIER. ALL WE NEED IS A KEYWORD. AND THAT IS "PARAMS" LET'S HAVE THE FUN REMEMBER, THE PARAMS PARAMETER MUST BE A SINGLE ARRAY.   public class MyClass         {                          public static void amarmethod(params int[] amararrayerkichuelements)             {                                 for (int i = 0; i < amararrayerkichuelements.Length; i++)                 {                     Console.Write(amararrayerkichuelements[i] + " ");                 } ...