Byte swap question (Bitwise operation) [on hold] The Next CEO of Stack Overflowsystem call to peek next byte from serial portTest question regarding grep
INSERT to a table from a database to other (same SQL Server) using Dynamic SQL
Do I need to write [sic] when a number is less than 10 but isn't written out?
Does increasing your ability score affect your main stat?
Why isn't acceleration always zero whenever velocity is zero, such as the moment a ball bounces off a wall?
Is it ever safe to open a suspicious HTML file (e.g. email attachment)?
Is wanting to ask what to write an indication that you need to change your story?
Can a Bladesinger Wizard use Bladesong with a Hand Crossbow?
Find non-case sensitive string in a mixed list of elements?
Is micro rebar a better way to reinforce concrete than rebar?
Why doesn't UK go for the same deal Japan has with EU to resolve Brexit?
How do I align (1) and (2)?
Why is the US ranked as #45 in Press Freedom ratings, despite its extremely permissive free speech laws?
Is French Guiana a (hard) EU border?
Does Germany produce more waste than the US?
Is it professional to write unrelated content in an almost-empty email?
Are police here, aren't itthey?
Dominated convergence theorem - what sequence?
What does "Its cash flow is deeply negative" mean?
What steps are necessary to read a Modern SSD in Medieval Europe?
How to count occurrences of text in a file?
If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?
Solving system of ODEs with extra parameter
0 rank tensor vs 1D vector
Is it convenient to ask the journal's editor for two additional days to complete a review?
Byte swap question (Bitwise operation) [on hold]
The Next CEO of Stack Overflowsystem call to peek next byte from serial portTest question regarding grep
I have seen some bitwise operation practice questions (in C language) and there was one I didn't understand: you had to swap the nth and the mth byte in a specifc integer "x" using only bitwise operations, and that n and m were both <=3. The solution involved masking the last 8 bits of the number resulting from xor based on x >> (n<<3) to x >> (m<<3), but the part I don't understand is why they shifted m/n right 3.
c
put on hold as off-topic by ilkkachu, Rui F Ribeiro, muru, jimmij, maulinglawns yesterday
- This question does not appear to be about Unix or Linux within the scope defined in the help center.
add a comment |
I have seen some bitwise operation practice questions (in C language) and there was one I didn't understand: you had to swap the nth and the mth byte in a specifc integer "x" using only bitwise operations, and that n and m were both <=3. The solution involved masking the last 8 bits of the number resulting from xor based on x >> (n<<3) to x >> (m<<3), but the part I don't understand is why they shifted m/n right 3.
c
put on hold as off-topic by ilkkachu, Rui F Ribeiro, muru, jimmij, maulinglawns yesterday
- This question does not appear to be about Unix or Linux within the scope defined in the help center.
add a comment |
I have seen some bitwise operation practice questions (in C language) and there was one I didn't understand: you had to swap the nth and the mth byte in a specifc integer "x" using only bitwise operations, and that n and m were both <=3. The solution involved masking the last 8 bits of the number resulting from xor based on x >> (n<<3) to x >> (m<<3), but the part I don't understand is why they shifted m/n right 3.
c
I have seen some bitwise operation practice questions (in C language) and there was one I didn't understand: you had to swap the nth and the mth byte in a specifc integer "x" using only bitwise operations, and that n and m were both <=3. The solution involved masking the last 8 bits of the number resulting from xor based on x >> (n<<3) to x >> (m<<3), but the part I don't understand is why they shifted m/n right 3.
c
c
edited 2 days ago
Rui F Ribeiro
41.8k1483142
41.8k1483142
asked 2 days ago
Goodwin LuGoodwin Lu
435
435
put on hold as off-topic by ilkkachu, Rui F Ribeiro, muru, jimmij, maulinglawns yesterday
- This question does not appear to be about Unix or Linux within the scope defined in the help center.
put on hold as off-topic by ilkkachu, Rui F Ribeiro, muru, jimmij, maulinglawns yesterday
- This question does not appear to be about Unix or Linux within the scope defined in the help center.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Because if n
is a position within the word, in bytes, then 8*n
is the same position in bits. And n<<3
is the same as 8*n
(23 = 8).
With n=1
, n<<3
is 8
, and (x >> (n << 3)) & 0xff
would move the second lowest byte to the bottom position, and mask the rest of the word out. (The bytes would numbered starting from zero, of course.)
(I'm assuming bytes of 8 bits, since I think that's by far the most common case at least nowadays.)
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Because if n
is a position within the word, in bytes, then 8*n
is the same position in bits. And n<<3
is the same as 8*n
(23 = 8).
With n=1
, n<<3
is 8
, and (x >> (n << 3)) & 0xff
would move the second lowest byte to the bottom position, and mask the rest of the word out. (The bytes would numbered starting from zero, of course.)
(I'm assuming bytes of 8 bits, since I think that's by far the most common case at least nowadays.)
add a comment |
Because if n
is a position within the word, in bytes, then 8*n
is the same position in bits. And n<<3
is the same as 8*n
(23 = 8).
With n=1
, n<<3
is 8
, and (x >> (n << 3)) & 0xff
would move the second lowest byte to the bottom position, and mask the rest of the word out. (The bytes would numbered starting from zero, of course.)
(I'm assuming bytes of 8 bits, since I think that's by far the most common case at least nowadays.)
add a comment |
Because if n
is a position within the word, in bytes, then 8*n
is the same position in bits. And n<<3
is the same as 8*n
(23 = 8).
With n=1
, n<<3
is 8
, and (x >> (n << 3)) & 0xff
would move the second lowest byte to the bottom position, and mask the rest of the word out. (The bytes would numbered starting from zero, of course.)
(I'm assuming bytes of 8 bits, since I think that's by far the most common case at least nowadays.)
Because if n
is a position within the word, in bytes, then 8*n
is the same position in bits. And n<<3
is the same as 8*n
(23 = 8).
With n=1
, n<<3
is 8
, and (x >> (n << 3)) & 0xff
would move the second lowest byte to the bottom position, and mask the rest of the word out. (The bytes would numbered starting from zero, of course.)
(I'm assuming bytes of 8 bits, since I think that's by far the most common case at least nowadays.)
answered 2 days ago
ilkkachuilkkachu
62.8k10103180
62.8k10103180
add a comment |
add a comment |