<SOLVED> how can I split 8 bits into 2 x 4 bits?

I am reading two thumb wheels into a 74hc165 shift register and then trying to split the resulting data.
I have

//74hc165 shiftin                 define where my pins are
int latchPin = 8;  int dataPin = 6;  int clockPin = 7;  int input;
void setup() {
    Serial.begin(9600);
  pinMode(latchPin, OUTPUT);  pinMode(clockPin, OUTPUT);   pinMode(dataPin, INPUT);
}
void loop() {
  //Pulse the latch pin, set it to 1 to collect parallel data and then wait
  digitalWrite(latchPin,0);   delayMicroseconds(20);
  //set it to 0 to transmit data serially and then wait
  digitalWrite(latchPin,1);    delayMicroseconds(20); 
 input = shiftIn (dataPin, clockPin, MSBFIRST);  digitalWrite(clockPin,1);  delay (500); Serial.println (input);

If I read the input as MSBFIRST I can get one thumb wheel to read correctly in the monitor and if I read it as LSBFIRST I can get the other but only if the one I am not reading is on '0'.
How do I split the data so that I can read both thumb wheels into separate variables?
thanks,
Bob.

  byte leftNybble = (input >> 4) & 0x0F;
  byte rightNybble = input & 0x0F;

Note: '>>' is the 'shift right' operator and '&' is the 'bitwise AND' operator.

John,
Thank you both for the exceptionally quick reply and the solution to my problem that is exactly what I wanted.

Also

byte leftNybble = input / 16;
byte rightNybble = input % 16;

The compiler will probably generate the same code

@OP

You asked for 4-bit; but, leftNybble/rightNybble does still contain 8-bit?

GolamMostafa:
@OP

You asked for 4-bit; but, leftNybble/rightNybble does still contain 8-bit?

There is no four bit datatype.

Then, strictly spesking, the OP should ask:

How can I split/transform 8 bits (b7 - b4 b3 - b0) into the following forms:
0000 b3 - b0
0000 b7 - b4

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.