Skip to content

shiftin() bad code size and speed #346

Open
@permaBox

Description

@permaBox

Hi,
shiftin() seems inefficient especially for small machines because of the use of "dynamic shifts" like (1<<i). and shiftout() needs a cleanup.
I propose a replacement with the functions below which have better code size and speed based on the use of "static shifts"! Keep going!

uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) {
	uint8_t val = 0;

	for (uint8_t i = 0; i != 8; i++) {
		digitalWrite(clockPin, HIGH);
		if (bitOrder == LSBFIRST)
			val = (val>>1) | (digitalRead(dataPin)<<7);
		else
			val = (val<<1) | digitalRead(dataPin);
		digitalWrite(clockPin, LOW);
	}
	return val;
}

void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val) {
	for (uint8_t i = 0; i != 8; i++)  {
		if (bitOrder == LSBFIRST)
			digitalWrite(dataPin, val & 0x01), val >>= 1;
		else
			digitalWrite(dataPin, !!(val & 0x80)), val <<= 1;
			
		digitalWrite(clockPin, HIGH);
		digitalWrite(clockPin, LOW);		
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions