+ 3

How to get nth bit from number?

26th Jan 2020, 9:33 AM
NoobTech2.0
23 Answers
+ 10
To get the 3rd bit: n >> 2 shifts the 3rd bit all the way to the right. (n >> 2) & 1 shifts the 3rd bit all the way to the right and then extracts the rightmost bit. In general you want to shift by `i - 1` to get the i-th bit.
26th Jan 2020, 10:15 AM
Schindlabua
Schindlabua - avatar
+ 3
Use bitwise & "and" with a mask where the Nth bit of the mask is set to 1.
26th Jan 2020, 10:19 AM
rodwynnejones
rodwynnejones - avatar
+ 3
and what do you say about a way to put this two-bit number in an array (each digit in a separate cell) and access it by the array index? a = [1,0,1,0,1]
26th Jan 2020, 10:39 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
Hello! you mean two bit calculus?
26th Jan 2020, 9:45 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
your question does not reveal the essence. Please, show an example
26th Jan 2020, 9:57 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
Do you index from zero or one? For a binary number, which bit has the lowest index in your idea? is it the left most bit, or the right most one? 10101010 Is bit 1 (left most) or bit 0 (right most) having index 0?
26th Jan 2020, 10:00 AM
Ipang
+ 2
this is already becoming clearer... what are your thoughts on this?
26th Jan 2020, 10:05 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
do you understand what Ipang asked you?
26th Jan 2020, 10:06 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
do you use bitwise shift to solve this problem?
26th Jan 2020, 10:18 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
Reed Richards just the same. according to the same scheme
27th Jan 2020, 10:28 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
Please specify a language in your question tags, you have taken multiple courses it is difficult to deduce a context in regards to which language 👍 (Edit) Specified language added in tags. Thank you for understanding 👍
26th Jan 2020, 9:50 AM
Ipang
+ 1
i mean, how to get nth bit from number
26th Jan 2020, 9:55 AM
NoobTech2.0
+ 1
do you have any thoughts on how to do this?
26th Jan 2020, 10:15 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
Thanks 🙂
26th Jan 2020, 10:18 AM
NoobTech2.0
+ 1
I was going to propose this, but Schindlabua's method is way neater 😆 Just as an alternative though, (n & (1 << index) ? 1 : 0) * Where <n> is the number, and <index> is N - 1, so <index> would be 2 if you want to get the 3rd bit.
26th Jan 2020, 10:35 AM
Ipang
+ 1
Third number from right it should be a[2]
26th Jan 2020, 10:43 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
NoobTech2.0, similar to what Ipang was saying: You shift a 1 to wherever you need it, and then you binary-or the results. n | (1 << index)
27th Jan 2020, 11:57 AM
Schindlabua
Schindlabua - avatar
+ 1
How to get nth bit from number for which language please specify
27th Jan 2020, 5:45 PM
Mohit Kumawat
Mohit Kumawat - avatar
0
There is number 21 (which is 10101), and i want to get (for example) 3th bit. How i would do it?
26th Jan 2020, 10:03 AM
NoobTech2.0
0
Yes, I am indexing from the most right one
26th Jan 2020, 10:09 AM
NoobTech2.0