Printing byte prints four bytes

I have this debug output sequence in my code

	// C++ does not use named parameters so, to make things more clear,
	//  we get the parameter values individually.
	// the 0..9 range references the variable coordinateByte[]
	row = random(0, 9);
	column = random(0, 9);

	// interval is any old time lapse
	interval = random(200, 2000);

	if (DEBUG)
	{
		Serial.print("row number is ");
		Serial.println(row, DEC);
		Serial.print("row pattern is ");
		Serial.println(~coordinateByte[row], BIN);
		Serial.print("column number is ");
		Serial.println(column, DEC);
		Serial.print("column pattern is ");
		Serial.println(coordinateByte[column], BIN);
		Serial.print("interval period is ");
		Serial.print(interval, DEC);
		Serial.println(" ms");
		Serial.println();
	}

(coordinateByte[] is defined as

// define data to write to matrix
int coordinateByte[] = {0, 1, 2, 4, 8, 16, 32, 64, 128 };

The debug printout I get is

Opening port
Port open
row pattern is 11111111111111111111111111110111
column pattern is 1
interval period is 353 ms

row pattern is 11111111111111111111111111101111
column pattern is 10000
interval period is 988 ms

row pattern is 11111111111111111111111111011111
column pattern is 100000
interval period is 1763 ms

row pattern is 11111111111111111111111101111111
column pattern is 10000
interval period is 1573 ms

row pattern is 11111111111111111111111101111111
column pattern is 1000
interval period is 216 ms

row pattern is 11111111111111111111111111110111
column pattern is 10000000
interval period is 358 ms

Why does row pattern show as four bytes? Is it that feral tilde ('~')?

I'm a bit confused by your variable names and an apparent mismatch between the debug statements in your code snippet and the output generated.

Usually, it is best to post your entire code.

It is true that an int datatype on an Arduino occupies two bytes. If you are doing bit manipulation, then you are better off using unsigned int rather than int. You can try casting the expression ~myInt to an integer type of your choice before printing it, say (unsigned int) ~myInt

6v6gt:
It is true that an int datatype on an Arduino occupies two bytes.

I changed the type to byte, but that didn't fix it.

FTR: deleting the feral '~' fixed it. I still don't understand why the tilde was there.

This code is modified from Boxall, J. (2013) Arduino Workshop. San Francisco (CA) USA: No Starch Press. p.143

// debug switch
const bool DEBUG = true;

// specify pins
#define dataPin 6 // connect to pin 14 on 74HC595
#define latchPin 8 // connect to pin 12 on 74HC595
#define clockPin 11 // connect to pin 11 on 74HC595

// define data to write to matrix
byte coordinateByte[] = {0, 1, 2, 4, 8, 16, 32, 64, 128 };

// matrix position variables
int row, column, interval = 0;


/*==========================================*/


// forward declaration(s)

// send lighting pattern to matrix
void setLEDs(int row, int column, int interval);


/*==========================================*/


void setup()
{
	// start Serial
	Serial.begin(9600);

	// set up pin status
	pinMode(latchPin, OUTPUT);
	pinMode(clockPin, OUTPUT);
	pinMode(dataPin, OUTPUT);

	// set up random seed
	randomSeed(analogRead(0));
}  // end setup()

void loop()
{
	// C++ does not use named parameters so, to make things more clear,
	//  we get the parameter values individually.
	// the 0..9 range references the variable coordinateByte[]
	row = random(0, 9);
	column = random(0, 9);

	// interval is any old time lapse
	interval = random(200, 2000);

	if (DEBUG)
	{
		Serial.print("row array number is ");
		Serial.println(row, DEC);
		Serial.print("row pattern is ");
		Serial.println(coordinateByte[row], BIN);
		Serial.print("column array number is ");
		Serial.println(column, DEC);
		Serial.print("column pattern is ");
		Serial.println(coordinateByte[column], BIN);
		Serial.print("interval period is ");
		Serial.print(interval, DEC);
		Serial.println(" ms");
		Serial.println();
	}

	// send lighting pattern to matrix
	setLEDs(row, column, interval);
}  // end loop()


/*==========================================*/


// send lighting pattern to matrix
void setLEDs(int row, int column, int interval)
{
	digitalWrite(latchPin, LOW);
	shiftOut(dataPin, clockPin, MSBFIRST, coordinateByte[column]);
	shiftOut(dataPin, clockPin, MSBFIRST, coordinateByte[row]);
	digitalWrite(latchPin, HIGH);
	delay(interval);
}  //  end setLEDs()

It gives the debug printout

Opening port
Port open
row array number is 0
row pattern is 0
column array number is 2
column pattern is 10
interval period is 855 ms

row array number is 8
row pattern is 10000000
column array number is 6
column pattern is 100000
interval period is 870 ms

row array number is 0
row pattern is 0
column array number is 2
column pattern is 10
interval period is 1065 ms

row array number is 8
row pattern is 10000000
column array number is 2
column pattern is 10
interval period is 1705 ms

row array number is 4
row pattern is 1000
column array number is 5
column pattern is 10000
interval period is 665 ms

row array number is 7
row pattern is 1000000
column array number is 5
column pattern is 10000
interval period is 1943 ms

BTW: the identifier coordinateBtye uses the word coordinate as in Cartesian coordinate. That might be the confusion with the identifiers.

Make row an unsigned int and try again.

From the reference page for ~ (bitwise NOT)

At times, the sign bit in a signed integer expression can cause some unwanted surprises.

vagulus:
. . .

FTR: deleting the feral '~' fixed it. I still don't understand why the tilde was there.

. . .

BTW: the identifier coordinateBtye uses the word coordinate as in Cartesian coordinate. That might be the confusion with the identifiers.

  1. The '~' : But have you looked at the output after deleting the tilda ? Surely it is completely different.

  2. coordinateBtye: Ah yes. I confused Btye and Byte so I thought it was not a good name for an int. My mistake

6v6gt:
2. coordinateBtye: Ah yes. I confused Btye and Byte so I thought it was not a good name for an int. My mistake

Oops! :-[

vagulus:
FTR: deleting the feral '~' fixed it. I still don't understand why the tilde was there.

Do you know what the tilde does?

It takes the bits in a value and flips them. All "1" bits become zero and all "0" bits become 1.

For example:

uint8_t var1 = 0x65; // this is 01100101 in binary
uint8_t var2 = ~var1; // 01100101 gets flipped to 10011010 which is 0x9A

Interestingly, the SUM of these two will always be 0xFF (255), no matter what the value is.

Hope this helps.

vagulus:
Why does row pattern show as four bytes? Is it that feral tilde ('~')?

You have to be winding me up. I responded to that exact question earlier this evening. Not only did you cross-post you did it so a moderator cannot help but notice.

Enjoy your timeout @vagulus.