Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Any positions Bits shifting to right

Mtech1

Junior Member level 1
Joined
Mar 18, 2023
Messages
17
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
164
I am having difficulties understanding how to shift bits that are not in sequence.

I understand shifting bits in sequence, like (7 to 4) or (7, 6). For instance, shifting the higher nibble to the right is achieved by using 'byte = byte >> 4,' resulting in 0000 11100.

Similarly, shifting only two bits (7, 6) to the right is done with 'byte = byte >> 2,' which gives 0000 0011.

However, I'm struggling to understand how to perform bit shifts for non-sequential positions like (6, 3) or (4, 2).

For example, if we take the byte 1100 0101 and want to shift the bits (6, 3) to the right, or (4, 1), it's not clear to me how to perform these bit shifts.
 
There is no mathematical solution but there are methods:
1. Look up table - big but very fast.
2. probably easiest - declare a new variable 'OutVariable' with value zero, do a bit by bit check and set the result in it. Something like this
Code:
if(InVariable & 0x01) OutVariable |= (1 << NewBit0Position);
if(inVariable & 0x02) OutVariable |= (1 << NewBit1Position);
if(inVariable & 0x04) Outvariable |= (1 << NewBit2Position);
.
.
if(InVariable & 0x80) OutVariable |= (1 << NewBit7Position);
That will rearrange any bit in 'InVariable' to a new position in 'OutVariable' in 8 instructions.

Brian.
 
Hi,

it always shifts all bits. There is no shift of only bits 7:4 or 7,6

a Logic Shift Right is:
* ALL bits are shifted one bit to the right ...
* the LSB is lost
* the MSB becomes 0.
using 'byte = byte >> 4,' resulting in 0000 11100.
there´s something wrong. One byte is 8 bits, but you show 9 bits.

Code:
   [ D7 D6 D5 D4 D3 D2 D1 D0 ]
      |  |  |  |  |  |  |  |
-->[  0 D7 D6 D5 D4 D3 D2 D1 ] (D0 = lost)

However, I'm struggling to understand how to perform bit shifts for non-sequential positions like (6, 3) or (4, 2).

For example, if we take the byte 1100 0101 and want to shift the bits (6, 3) to the right, or (4, 1), it's not clear to me how to perform these bit shifts.
Please draw a sketch of
* the input byte,
* the expected output
* and describe which bits should move
* and how you expect the other bits to behave

Klaus
 
Last edited:
I think I'm overthinking the problem, and I need to keep it simple. Sorry for wasting your time. I thought this might be a common issue dealt with in embedded systems, but I realize now that it doesn't make sense to dwell on it. I believe I've done enough with bitwise operators, and it's time to move on to the next topic.
 
Certainly, understanding bit shifts can indeed be tricky, especially when dealing with non-sequential positions. Let's break down how to perform bit shifts for positions like (6, 3) or (4, 1).

For shifting bits (6, 3) to the right:

  1. First, create a mask for the bits you want to preserve. In this case, the mask would be '0011 1100.'
  2. Apply the mask using a bitwise AND operation to preserve the bits you want: 'byte = byte & 0011 1100.'
  3. Next, shift the preserved bits to the right by 3 positions: 'byte = byte >> 3.'
For shifting bits (4, 1) to the right:

  1. Create a mask for the bits to preserve. The mask for this case would be '0001 1110.'
  2. Preserve the bits you want using bitwise AND: 'byte = byte & 0001 1110.'
  3. Shift the preserved bits to the right by 1 position: 'byte = byte >> 1.'
In essence, when dealing with non-sequential positions, you use masks to isolate the bits you want to preserve, perform the shift on the preserved bits, and then combine the results if needed. This approach allows you to selectively manipulate specific bits within a byte, even if they are not adjacent.
 
Basic problem of the original question is lack of clarity. As stated by KlausST, it doesn't tell how the value of each output bit shall be set.
For shifting bits (6, 3) to the right:

  1. First, create a mask for the bits you want to preserve. In this case, the mask would be '0011 1100.'
Despite of ambiguity, post #1 tells at least that "bits(6,3)" refers to non-sequential bits in contrast to e.g. "bits(7 to 4)". Thus the suggested bit masks can't be right.
 

LaTeX Commands Quick-Menu:

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top