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 :
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 = 1Okey, let's see the same example :24 -> 0001 10004 -> 0000 0100res-> 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 10004 -> 0000 0100res-> 0001 1100Hey!! 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 ⇒ 20102Do 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 ⇒ -11Computer 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
Post a Comment