C_18 Operators in C - Part 6 (Bitwise Operators) | C Programming Tutorials

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the series of learning programming in c in previous videos i have covered arithmetic operators assignment operators increment and decrement operators logical operators and relational operators with examples in detail now in this video i'll talk about bitwise operators in c what are bitwise operators types of bitwise operators why to use these operators and how to use these operators the syntax the meaning and when you use these operators in your program then what type of output you will get everything we will be discussing in detail about bitwise operators with some examples you can say with the program right and this video is brought to you by an academy so uh if you are interested in competitive programming then an academy in collaboration with code chef has started a batch to become an expert level programmer it's like you can say a complete course uh for the intermediates to become an expert level programmer and this batch has already been started from 22nd of december so do join as soon as possible and in this batch you will get a structure learning to become an expert level programmer and highly competent instructors will take your classes having that icpc world finalist tag and you can see the schedule of the batch here the date the name of the educator and the topic they'll cover on that day so and more courses will be added soon as the batch progress and this course will help you to develop end-to-end subject matter expertise which is required to get into big tech companies you can start your own tech companies and which would would be helpful to crack any international coding contest and if you go for the subscription for six months or one one year subscription then after successful completion of the course you will get industry accepted code chef certification also and you can attend live interactive classes with their top educators and in each class with the theory concepts some practice problems would also be discussed which will help you to apply your knowledge in real time and if you get stuck somewhere after the class then you can get help from the teaching assistant from the community community members the expert level community members instantly and this is paid subscription so if you are interested you can go for it as you can see the pricing detail here if you use the code jkl10 you will get 10 discount on the these paid subscription so if you are interested then all the details and the link as well as the referral code i'll put in the description box of this video you can go and check out so now let us discuss what are bitwise operators in cc as the name suggest bit we have bit in this one bit wise so these operators are used to perform operations at bit level what is bit bit is what the smallest unit storage unit in computer memory we have first of all bit then we have byte byte is what equal to 8 bits right the bit is the smallest you need to store some data in computer memory and after that you have kilobyte and then megabyte gigabyte a terabyte like this so bit is what the smallest unit to store data in computer memory and using these operators we can perform in c we can perform bit level expression we can manipulate you can say data at bit level although in alu in arithmetic logic unit we though in cpu we have that logical unit there every mathematical and logical operations are executed so those operations are obviously at a bit level because in computer memory we cannot store directly like five plus four so we cannot directly store five and four these are stored in binary forms zeros and one one forms right either in four bits or eight bits and generally 8 bits right but in c also in when we do programming in c then also we can perform bitwise operations we can perform operations at bit level so in this case we can apply these operators on integer values and character values but we cannot apply these operators on float and double values right now types of bitwise operators we are going to discuss one by one so these are bitwise operators bitwise and bitwise or bitwise xor wise not or you can say once complement this is a left shift and this is right shift and these are binary operators we require two operands for these operators this one is this bit wise not this one is unary operator we require only one operand to perform this operator right and see if you write like this 5 plus 4 then or if you write 5 and 4 then these type of operators minimum inputs for these type of operators are what a byte they perform at byte level eight bits minimum uh that requirement minimum input you can give off eight byte sorry eight bits that is one byte right now first of all discuss what is that bitwise and operator so it is binary operator so it requires two operand let us suppose i am writing here a bitwise and b and i am taking into a is equal to 10 and b is equal to 5. so now it is different from logical end there we use double end double ampersand so now first of all in bitwise operators the data would be converted into binary form the decimal a value would be converted at binary form then the operation is to be performed on that data after that you will get result and then result would be in binary form and then the result would be converted into decimal form and then the output would be printed to you so the output of these operators are what integer value any decimal value and output of logical and this operator is what either true or false means zero or one but here we are not getting true or false we are getting proper output so now first of all see prerequisite of this is what you should know how to convert a decimal number to binary number you should know how to convert a binary number to decimal number if you don't know then maybe we can discuss in some other video when we are going to discuss number system so now first of all convert 10 see 10 and 5 but we cannot perform directly 10 should be converted to binary 5 should be converted to binary now the binary value of 10 is what 1 0 1 0 i'm using 4 bits to represent a number c like this i am just writing down the shortcut here 2 raised to the power 0 2 raised to the power 2 to this to power 2 2 raised to the power 3 so 2 0 is 1 it is 2 it is 4 it is 8 like this 2 raised to power 4 and like this you can write these numbers but i am using 4 bit representations in 4 bit representation we can take maximum number as minimum is 0 0 0 means 0 and maximum is 1 1 1 1 we can put every place at 1 so now when you will add these numbers then it would be 15 in decimal so i am taking numbers from 0 to 15 i'm not taking greater than 15. if you take greater than 15 then what you can do you can again write 2 raised to the power 4 that is 16 36 64. like this you can write and you can take any number greater number also but i will take only number less than 15 right and this is short cut this is how you we will convert from decimal to binder and binary to decimal so now these are places now 10 so where you will write down 1 1 0 0 so that the addition of these numbers will become 10 8 plus 2 is 10 so this is 10 in binary 5 in binary is what here you place 1 here you place 1 4 plus 1 is 5 at other places remaining places i'll put a 0. so this is 5 right we have converted into binary form now apply this logical and operator in on these so at a bit level we are going to perform this logical end now what is the output same here you when you apply logical end then what was the truth table for logical and if you have 0 0 then you will get 0 if you have 0 1 then 0 1 0 then 0 if you have 1 1 then only you will get 1 if both the places are 1 then only you will get 1 right same you apply that logic here we have 0 and 1 0 1 0 0 0 and 0 and when you convert this this binary when you convert this into decimal then it would be 0 because there is no 1 so you cannot add any number so finally output would be 0. so the output would be 0 you will get 0 output that is bitwise and operator and we cannot perform these operators on minus numbers negative numbers right now see if you store like this c is equal to a bitwise and b then what you can see after execution of this statement each bit in c will be one only if the corresponding bits in both a and b are one both a and b are one otherwise bit would be zero here we don't have both bits in a and b is uh are one so it is zero so this is what bitwise end operator now what about bitwise or operator i'm taking same example and this is bitwise or or when you will use that laptop or that key then this the key is like this this and this right now what is bitwise or operator here logical or is this one so what was truth table of logical or operator this is for end for logical or it is zero if any one is one then you will get one if both are zero then it is only zero so at a bit level you can say ultimately we are applying logical or but at a bit level right and it and at a bit level we are applying the concept of logical or right now see 10 is this one 5 is this one is binary form 10 or that logic bitwise or and 5 what output you will get 0 1 1 this is 1 this is 1 this is 1 at every place you are getting 1 now convert this into binary at every place we are having 1 so add all the numbers this is having the number is 15 so final output you will get 50 right and if you write like this 10 logical or five then output you will get one that is true because this is true this is true and here if you you will use like and then also you will get output one this is true this is true but here you are getting a number decimal number so this is the difference between these operators logical operators and bitwise operators now next is a bit wide bitwise xor operator now same here if i store this one into c then what you can say after execution of the statement each bit in c would be one only if the corresponding bits in either a or b is one otherwise it is zero right now but why bitwise xor operator same example i am taking here this one a and b the values are same so now a xor b now what you will get see the table of this xor is what if both the bits are same then output would be zero zero if both the bits are different then you will get output one right so this is basically you can say to toggle the bits toggle means either the bit is 0 then it would be 1 if the bit is 1 it would become 0 right so now see ax or b here same a is having this value in binary b is having this value in binary now apply xor operations here zero one both beta bits are different so you will get one different one different one different one right so the final output would be same this also in decimal it could be fifteen so output you will get 50. so i hope you got the meaning of these operators and these are decimal values if you apply these operators on character values then character is also like this one a then a would be first of all mapped to the sky value that is 97 97 would be converted into binary number so we cannot store the 97 using four bits because maximum number can be stored can be 15. so we will use 8 bits 8 bits means here 16 here we have 32 here we have 64. and you can write down here 1 means addition of if you write one then addition of those numbers here those places should be 297 so where you can put write down one here like this here and here here and here 64 plus 32 is 96 and all the bits would be 0 and this bit would be 1 96 plus 1 is 97 like this so you can use character also so now let us take one example with a program i am going to discuss this all the these type of operators the three operators so let us let us take this program no need to tell i guess output of this one this one and this one because i have already discussed value is here six and a is 5 so you can tell me in comment box let me discuss this statement in this expression i am having multiple operators bitwise and also logical and also logical or also relational operator also now how the statement would be executed if in expression we have multiple operators check the precedence first of all precedence of this relational operator is higher then the priority of bitwise operators bitwise end then logical end then logical or operator right so first of all what would be converted what would be executed the operator which is having higher priority that is 1 that is plus so here b plus 1 b is what 6 so value becomes 7 right now priority of bitwise and is higher than logical operators so now this operator a and b a is 10 b 6 first of all converted into binary form then bitwise and means if both the bits of a and b are 1 then only it will give 1 otherwise it will give 0. so the final output i am writing here you can write down you can check out by converting it into binary and then the result you can convert into decimal the final output you will get 2 2 you will get here we have 7 logical end or 0. now this is the expression now logical line is having higher priority so this would be executed first 2 and 7 both values are true values because these are not 0 true true means it will give true that is 1 1 or 0 if one is value is 1 about logical or operator then final output you will get one so it will print one and suppose if i update the statement like this after this i am writing here logical or and i am writing here um b plus plus so now this is post so original value would be used here in this expression and after the execution of this line after the semicolon after completion of the execution then the value can be incremented in post so b value 6 so 6 would be used so here i have this one and 6 value right everything is same so now this operator is having higher precedence so this is one logical or here zero here we have logical or end six logical or having same precedence associativity check the associativity that is left to right here left to right this would be executed first now first operand is one so it will not check the second one because if logical or open if in logical or the first operand is one final it will it will give one so it will not check this one then or and here we have b plus plus b plus plus value is six now the first operand for this is one so it will not execute the second one final output would be one so the output would be one but if after this if you will print value of b then what value you will get you will get 6 you will not get 7 why because this this expression is has not been executed because the first operand was 1 so this was not executed that is why no b plus plus only you will get 6 right if you maybe if you replace this by and then maybe you can get b plus plus that is 7. so you can tell me in comment box if i write here and operator logical end then what output you will get first of all output of the statement either one or zero and after that value of b what out what value you will get right so i hope you got the basic idea of bitwise operators now in next video i will discuss these three remaining operators bitwise not once complement left shift and right shift operator with the help of a program if you have any doubt in bitwise operators you can tell me in comment box maybe i can make a separate video and with that proper examples with some difficult type of example tricky examples right so now that's it for this video now i'll see you in the next video till then bye take you
Info
Channel: Jenny's lectures CS/IT NET&JRF
Views: 46,046
Rating: 4.8846846 out of 5
Keywords: data structure tutorials, operating system, data structure and algorithms, jayanti khatri lamba, jennys lectures, jenny data structures, jennys lectures DS, jenny lamba, jennys baby, jennys lectures baby, data structures, operators in C, bitwise operators, bitwise operator, jenny lecture, jennys lecture, jenny lecture data structure, jenny lecture cs it
Id: egUyaWtsQc0
Channel Id: undefined
Length: 17min 8sec (1028 seconds)
Published: Sun Dec 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.