What is the result of "i%0xff"?

how to pronounce result
what is the antonym of result
results online
define consequence
getting results synonym
final result synonym
define outcome
the result of an action is called

So I found this in some bluetooth code and I would like to know what it does.

for (int i = 0; i < sizeof(notify_data); ++i) {
    notify_data[i] = i%0xff;
}

The result is 0x{00 01 02 03 04 ...} but how and why?

This code is actually probably wrong.

Probably the author meant i & 0xff or i % 0x100 as i % 0xff` does not make too much sense in this scope.

He wanted to save only the lowest 8 bits of the data.

The module % operation returns the remainder from the integer division.

Assuming x and y are integers

x % y = x - y * (x / y) 
EDIT
for (int i = 0; i < sizeof(notify_data); ++i) {
    notify_data[i] = i%0xff;
}

This code has a very low quality. The issues:

  1. i has to be size_t not int

  2. sizeof(notify_data) it one of the most common beginner errors, as sizeof does not give the runtime size of the object. It is evaluated the compile time and as we see here on SO many beginners think that it works like strlen or returns the size of malloced memory block

example:

char x[100];
char y[200];

void printsizes(char x[])
{
     printf("sizeof(x) = %zu, sizeof(y) = %zu\n", sizeof(x), sizeof(y));
}


int main(void)
{
     printsizes(x);
}
  1. wrong % use.

As A Result, As a result definition is - because of something —often + of. How to use as a result in a sentence. Result definition is - to proceed or arise as a consequence, effect, or conclusion. How to use result in a sentence.

Since % is the operator that returns the remainder from the integer division. It means that this code is right! And makes the array data equal to i up to "8 bit max value" and then repeat the cycle. It is just a protections in order to not overflow the 8 bit value!

Thank you guys ;)

RESULT, result definition: 1. something that happens or exists because of something else: 2. because of something: 3. a good…. Learn more. That was the result, he thought, of his absurd whim of loitering about Berry town. Sure of the result, he pressed with his finger tips upon the lower end of that short piece of board. They were driven out as the result of a Russian counterattack. From the blood, thus imperfectly purified,

Fills up the array with values from 0 to 0xff. If the array is larger than 0xff elements, it starts over again from 0. Will be something like: {0, 1, ... , 254, 0, 1, .. 254, 0, 1,...} and so on.

What is result? definition and meaning, Definition of result: Outcome, consequence, or conclusion of a problem, probe or experiment after a period of time. This conclusion can be one result, multiple  A result (also called upshot) is the final consequence of a sequence of actions or events expressed qualitatively or quantitatively. Possible results include advantage, disadvantage, gain, injury, loss, value and victory. There may be a range of possible outcomes associated with an event depending on the point of view, historical distance or relevance.

this is the result of | English examples in context, High quality example sentences with “this is the result of” in context from reliable sources - Ludwig is the linguistic search engine that helps you to write better in  A result of something is an event or situation that happens or exists because of it. The result of this announcement was that the price of the company rose by 10 per cent. I nearly missed the flight as a result of getting stuck in traffic. I cut my own hair – often with disastrous results.

it is a result of | English examples in context, High quality example sentences with “it is a result of” in context from reliable sources - Ludwig is the linguistic search engine that helps you to write better in  Define results. results synonyms, results pronunciation, results translation, English dictionary definition of results. intr.v. re·sult·ed , re·sult·ing , re·sults 1. To happen as a consequence: damage that resulted from the storm; charges that resulted from the

the results of which are | English examples in context, High quality example sentences with “the results of which are” in context from reliable sources - Ludwig is the linguistic search engine that helps you to write  For example, in the Results section of a scientific report, a study that finds men with waist sizes over 36 inches being at high risk for diabetes should state merely that. The implication that men with big waists should lose weight to prevent diabetes is not a result but a suggestion based on the result.

Comments
  • i % 0xFF will produce values from 0 to 254 inclusive. 0xFF is hexadecimal for 255. % is the modulus operator.
  • Be carefull as the quality of the code taken from internet is very often low. Do not learn the language from the internet examples.
  • Probably it is just a bug and it should be i % 0x100 instead. Though if you don't know what % does in C, it is time to read a book.
  • I wonder if this is why my iPhone4 doesn't work in my wife's Porsche?
  • @MrKovalKoval: In the %.2f context, % is not acting as an operator, it's in a quoted string. It's little more than a convention used by the printf type functions.
  • 8 bytes -> 8 bits.
  • @Bathsheba :} phone spell checker
  • oh, nice! you answered my question! I didnt realise that it was % operator. +1 for you! (but someone downvoted so I cant +1)
  • @MrKovalKoval I've just upvoted you; you're >= 15 Get in quick!
  • @P__J__ Do you want to edit your answer so I accept it? because it is not wrong!
  • Off by 1 "i up to 255" should be "i up to 254". Further, it is not so much a "protections in order to not overflow the 16 bit value". It might be a "protections in order to not overflow the 8 bit value", but it does a questionable job at that.
  • The result of i%0xff (int % int) is -254 to 254, depending on i. Never 255.
  • Protection not to overflow a 16 bit value should be properly written as value & 0xFFFF. Everything else is obfuscation.