-2

I'm writing a memory manager in C and am trying to make sure it's properly aligned (make sure the user space starts on an address divisible by 8, and make sure the whole block is divisible by 8 as well).

I was wondering if anyone could tell me what this does:

x = ((x - 1) | 7) + 1;

It's a code fragment that was suggested to me by a friend, but I'm unsure of what it's doing / what the vertical bar's function is in this scenario

Edit: I realize I could've explained this a bit clearer; x is an int, and I did look up that it was a bitwise OR operator, but I didn't understand what that meant in this context. Thanks for the help!

Curtis
  • 37
  • 7
  • 3
    Stackoverflow is not the best place for learning language basics. Consult a basic C tutorial or book. As for your questisn specifically, `|` is the [bitiwise OR operator](https://en.wikipedia.org/wiki/Bitwise_operations_in_C). – kaylum Mar 19 '16 at 04:10
  • Why didn't you just ask your friend? – Crowman Mar 19 '16 at 04:25
  • 1
    That looks wrong. At least using `int` values is a bad idea. – too honest for this site Mar 19 '16 at 04:25
  • 1
    @kaylum I don't think this is really "language basics", and how to correctly write expression to align memory, or understand an expression to do it, is certainly not trivial. I think this is a valid question, even if it could be a bit clearer. – hyde Mar 19 '16 at 09:17
  • @Curtis What is type of `x`? Is it supposed to be length or address of the block? – hyde Mar 19 '16 at 09:19
  • @Curtis ...also, it would have been good if you had checked list of C operators to know the proper name for the `|` operator... For example from [Wikipedia](https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B). – hyde Mar 19 '16 at 09:23

2 Answers2

2

A standard way to make a number divisible by 8 is:

len = (len + 7) & 0xfffffff8; /* for positive 32-bit values */

That should be easier to understand than your friend's construct (which BTW probably works as well, but see below).

The construct you have sets the lower 3 bits of your number by bitwise ORing it with 7 (thus creates a number that has a remainder of 7 when divided by 8), then adds 1 to make it divisible by 8. What the -1 means, you should work out on your own. I wouldn't even look at it nor use it if you don't anderstand what it does at first glance.

Whether it is advisable to use signed integers as addresses and block lengths you will surely get some other comments.

tofro
  • 5,640
  • 14
  • 31
0

This statement is in an if statement, like so:

if (x % 8 != 0):
    x = ((x - 1) | 7) + 1;

I should've just included more in the code fragment initially, apologies for that. Once I reviewed my binary math a little bit, I recognized that the "| 7" meant OR with 0111, and given that this is a statement used when x is thought to be misaligned, the results can only be as follows:

if (x-1) < 7 : ((x-1) | 7) is 0111. Adding 1 gives 8, which satisfies the condition.

if 15 > (x-1) > 7 : ((x-1) | 7) is 1111. Adding 1 gives 16, again satisfies

and so on for higher values.

Thanks for your suggestions everyone, this was my first question, I'll be sure to improve them in the future!

Curtis
  • 37
  • 7