Java Essentials - Bitwise operators in java
375 views
Nov 18, 2024
Java Essentials - Bitwise operators in java
View Video Transcript
0:00
B2WIS operators in Java. So in our discussion we shall discuss multiple
0:06
different B2S operators which are available in our Java coding and programming
0:11
So B2S operators are used to perform and or and these operations and also the
0:18
shipping operations will be performed using the B2s operators. So here we are having a
0:23
list of some B2S operators which are available in our Java. So this is that not, there is a
0:29
bit-wise complement, that means it does once complement of the respective operant
0:36
This is our and, so binary and operator. This is our filter sign which is denoting the or, that is the binary or operator
0:44
We're having this upper carrot. So this symbol is also known as upper carrot
0:48
It is used for binary zor operator, that means exclusive or. We're having this left-shift operator, we are having this right-shift operators
0:57
So these are the most usable beatwise operators which we use in our Java programming and coding on a very regular basis
1:06
So to have the clear conception, let us go for one demonstration using this beatwise operators
1:12
We will be writing our code, we'll be executing and we'll be explaining more
1:16
So here is the demonstration for you. Here in this demonstration we are discussing beatwise operators available in Java
1:24
So we have defined two variables, Val 1 and Val 2 with the value. is 12 and 10 and they are of the type of byte so the binary representation of 12
1:33
in byte that means in 8 bits we have written and also the binary
1:37
external of 10 in 8 bits we have mentioned that one so byte results so we are defining another variable result here so it has been represented as RES so result is equal to byte of knot val 1 that is a beat complement val 1 so this is
1:55
known as the not also the operated is known as tilt operator and we are
1:59
converting it to that byte type and assigning to this result now what is happening
2:04
here so this val 1 is actually containing 12 that means it is a binary
2:09
representation of 12 so 12 means 8 plus 4 that means at the eight and four place will be having one and rest bits will be filled up
2:16
with zeros so now to get that once complement of it we're getting this one as
2:21
four times one and then zero one so that is a knot of value one so if I print
2:27
this one we're getting here minus 13 now question is coming in mind why it is
2:32
minus 13 why the value has got printed as minus 13 we know that when the first
2:37
bit is one then the number will be treated as a negative quantity and to get the
2:42
magnitude I should calculate its two's complement so two's complement means once complement that means zeros will be replaced by one and once will be
2:50
replaced by 0 plus 1 and that is a once complement plus 1 will be the 2's complement
2:55
which will be denoting the magnitude part of the number and the first bit will be
2:59
denoting the sign that is our negative because of us bit is 1 so now what is this
3:04
this is our 12 plus 1 so that is a 13 so minus 13 will be the outcome and that we are
3:10
getting here as the outcome here next one is val 1 and val 2 so and here we are doing the bitwise and
3:18
operator so it will be denoted by this am percent so now if we do the bit wise
3:22
thus you can do for the columnar just see so here only we're having this
3:26
one and one so that why it this place will produce one here and rest all places will be producing 0 so this one means at the eighth place we having one so if we print we can find that after doing this ending we getting the 8 as the outcome in case of ore what is happening if you go on
3:45
oaring beatwise if you go on awring you can find that it is 1 or 1 is 1 or 0 is 1 and
3:55
the rest of the bits will be 0 so 1 1 1 1 0 will be the outcome so that means 8 plus
4:00
4 12 plus 2 that is the 14 here so that's why after awning we're getting this one as 14
4:07
next we're going for this zor operation in case of zor it will be denoted by this
4:12
upper carrot that is the operator that is a zor operation B2 by zord operation so what will be the outcome
4:19
you know that in case of zor operations similar bits will produce output 0 and this similar bits will produce the output 1
4:26
so here i'll be getting 1 because 1 and 0 Zor is 1 and this one zor 0 so zero Zor 1 is 1 so rest of the bits will be producing zero after zawing B2 is Zodding so 1 1 1
4:41
So 1 1 will be the outcome so I am expecting 6 as the result yes we're getting six after doing the Zodding
4:48
Next one is the this shift two bits towards the left that is a val 1
4:53
So here you are having this val 1 so I'm just shifting it towards the left for two bits place
4:58
so I'm just going forward so if I go for this to be it will be coming like this
5:04
that is towards the left so these two bits will be off will be lost and two
5:14
new bits will be coming to fill up the blank space so we can find that it will be having the format like this and which is nothing but here is nothing but 32 plus 16 and that should be 48 so we are expecting that the
5:29
value will become 48 here so now let me go for the execution you can find
5:34
that the value has become 48 actually what is happening the beat which is at the
5:39
eighth place with the place value 8 has been has been transferred to the place
5:44
value 32 the beat which was at the place value 4 has been transferred to the place value 16 so actually we are doing 2 to the prop 2 multiplication
5:55
actually we are doing 4 multiplication that means 2 to the part of 2 multiplication what is 2 to the
5:59
part of 2 this 2 is actually denoting that for how many bits we're shifting towards the left
6:05
that means if you shift towards the left for n number of bits then the magnitude will get increased
6:10
by that by the factor of 2 to the part of n so that's that's why 12 into 2 to the part of 2 that means 12 into
6:18
4 so 48 will be the outcome so that's why we're getting here 48 as outcome next one is that we're going for this value 1 which will be shifted towards the right and 2 bits towards the right then what will happen so these 2 bits will be lost and extra 2 is coming up so this 2 bits will produce 2 and 1 so that means I'm going to get here 3 so that's why we're getting the outcome as 3 so what is happening if we go for n bit in bit right shift
6:48
then actually the number will be divided by 2 to the part of n so here the value of n is
6:53
equal to because we are doing two bit shift so 2 to the part of 2 is 4 so 12 by 4 so 3
6:59
will be the result here so in this way we have explained the bitwise operators
7:04
which are available in Java with proper demonstration and example thanks for watching
7:10
this video
#Computer Science
#Java (Programming Language)
#Other