# What is the function of the tilde?

My current project contains this code

``````// 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()
``````

Boxall, J. (2013) Arduino Workshop. San Francisco (CA) USA: No Starch Press. p. 143

coordinateByte[] is defined as

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

What is the function of the tilde character ('~') in the code?

Thanks

Hi,
Does it compile?

It looks like a typo error.

Tom... :o

Try looking up C operators on Google

Pete

el_supremo:
Try looking up C operators on Google

I had looked up C++ operators but found nothing. FTR: A search for C operators shows '~' to be a bitwise 'NOT' which is defined thus:

The bitwise NOT, or complement, is a unary operation that performs logical negation on each bit, forming the ones' complement of the given binary value. Bits that are 0 become 1, and those that are 1 become 0. For example:
NOT 0111 (decimal 7)

• = 1000 (decimal 8)*

In this context, then, it matters not whether it is used or not. All it does is vary the bit patterns sent ro the matrix.

Thanks

vagulus:
In this context, then, it matters not whether it is used or not. All it does is vary the bit patterns sent ro the matrix.

so, if you send that.... does that mean that Neo isn't the One ?

does that mean that Neo isn't the One ?

vagulus:
I had looked up C++ operators but found nothing. FTR: A search for C operators shows '~' to be a bitwise 'NOT'

If you'd looked in the Arduino language reference (top of the page under "Learning") you'd have found something very similar. Being rather new to all this myself I find that a rather useful starting point for understanding things in Arduino programs.

Steve

Isn't it also a C++ destructor?

cossoft:
Isn't it also a C++ destructor?

Here's something it does:

Code snippet

``````	byte foo = 0;
Serial.print("foo is ");
Serial.println(foo, BIN);
Serial.print("~foo is ");
Serial.println(~foo, BIN);
``````

Output

``````foo is 0
~foo is 11111111111111111111111111111111
``````

How does tilde turn one byte into four bytes?

vagulus:
How does tilde turn one byte into four bytes?

It doesn't.

There is no println for byte-sized values. The smallest integer size supported by println is long. The complemented value is sign extended to a long before the call to println.