C
C#2y ago
Raki

❔ How bit wise shift operation work

I started learning bit wise operation. So i have a uint = 0101 bit which is int 5. Which is in loop to see how it works Consider n =0101 1st iteration var test = (n >> 0) // The value is 101 2nd iteration var test = (n >> 1) // The value of test is 50 How this Happens what i assumed is 0101 on 2nd iteration will become as 1010 so it will be 9. But how it becomes 50.
5 Replies
WhiteBlackGoose
n >> 0 doesn't do anything also you seem to have confused a bit binary and decimal representations you wrote 101 in decimal form so when you shift it right by one bit, it becomes 50 (gets divided by two is the same as shifting one bit rightward)
MODiX
MODiX2y ago
Oryp4ik#0120
REPL Result: Success
var a = 0b1101;
Console.WriteLine(Convert.ToString(a, 2));
Console.WriteLine(Convert.ToString(a >> 1, 2));
Console.WriteLine(Convert.ToString(a >> 2, 2));
Console.WriteLine(Convert.ToString(a >> 3, 2));
Console.WriteLine(Convert.ToString(a >> 4, 2));
var a = 0b1101;
Console.WriteLine(Convert.ToString(a, 2));
Console.WriteLine(Convert.ToString(a >> 1, 2));
Console.WriteLine(Convert.ToString(a >> 2, 2));
Console.WriteLine(Convert.ToString(a >> 3, 2));
Console.WriteLine(Convert.ToString(a >> 4, 2));
Console Output
1101
110
11
1
0
1101
110
11
1
0
Compile: 558.327ms | Execution: 38.379ms | React with ❌ to remove this embed.
WhiteBlackGoose
as you can see, it works but I kept everything with binary base
MODiX
MODiX2y ago
Oryp4ik#0120
REPL Result: Success
var a = 0b1101;
Console.WriteLine(a);
Console.WriteLine(a >> 1);
Console.WriteLine(a >> 2);
Console.WriteLine(a >> 3);
Console.WriteLine(a >> 4);
var a = 0b1101;
Console.WriteLine(a);
Console.WriteLine(a >> 1);
Console.WriteLine(a >> 2);
Console.WriteLine(a >> 3);
Console.WriteLine(a >> 4);
Console Output
13
6
3
1
0
13
6
3
1
0
Compile: 542.355ms | Execution: 30.110ms | React with ❌ to remove this embed.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server