# From C to Julia: Notes from a low-level port

**URL:** https://discourse.julialang.org/t/from-c-to-julia-notes-from-a-low-level-port/54743
**Category:** General Usage
**Created:** [February 6, 2021, 4:53pm UTC](https://discourse.julialang.org/t/from-c-to-julia-notes-from-a-low-level-port/54743 "2021-02-06T16:53:41Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![adolgert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adolgert/32/20286_2.png) [@adolgert](https://discourse.julialang.org/u/adolgert)
#### Post date: [February 6, 2021, 4:53pm UTC](https://discourse.julialang.org/t/from-c-to-julia-notes-from-a-low-level-port/54743/1 "2021-02-06T16:53:41Z")

</div>

I ported bit-shifting math from C to Julia and thought I would share notes on gotchas.

## Simple changes in operators

- Division - The solidus character, `/`, becomes div in Julia, `÷`. The `/` in Julia is floating-point division, so it will promote integers to reals.
- Exclusive or - The caret character, `^`, becomes the xor symbol, `⊻`. The `^` in Julia is exponentiation.

Both of these operators are will compile fine but have different meaning, so they are easy to mis-type and not notice.

## For-loops change scope and increment

In C, the variable `j` exists after the loop.

```C
	for (j = 1; j < DIM; j++)
		if ((P >> j & 1) == (P & 1))
			continue;
		else
			break;
	if (j != DIM)
		J -= j;

```

In Julia, for-loops have a new scope for the loop variable.

```julia
j = -1
for j = 1:3
    println(j)
end
println(j)

```

This will print 1, 2, 3, -1. We can rewrite the for-loop as a while-loop, which is generally handy when C for-loops do lots of initialization and increments.

```julia
j = 1
while j < DIM
	if ((P >> j & one(P)) == (P & one(P)))
		j += 1
	else
		break
end
if j != DIM
    J -= j
end

```

## Operator precedence changes

C and Julia group mathematical operations differently. They put implicit parentheses in different places. For instance, this small expression means something different in both languages.

```C
temp2 = S << DIM - xJ % DIM;

```

The operator precedence in C is 1. `%`, 2. `-`, 3. `<<`, 4. `=`.  
In Julia, it’s 1. `<<`, 2. `%`, 3. `-`, 4. `=`. We could put in all of the parentheses, or just those we need.

```julia
temp2 = S << (DIM - xJ % DIM)

```

Maybe we can look at a few operators, listed from high precedence to low, left-to-right, to pick out differences.

```julia-auto
C precedence for select operators.
(* / %) (+ -) (<< >>) (< <= > >=) (== !=) (&) (^) (|) (&&) (||) (= += -= <<= >>= &=)
(<< >>) (* / %) (& ÷) (+ - | ⊻) (> < >= <=) (== !=) (&&) (||) (= += -= &= <<= >>=)
Julia precedence for select operators.

```

The bit-shifting operators move above addition and subtraction. The XOR and AND operators move above greater-than and less-than. In general, it’s good to double-check the C precedence.

HTH!

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [February 6, 2021, 5:10pm UTC](https://discourse.julialang.org/t/from-c-to-julia-notes-from-a-low-level-port/54743/2 "2021-02-06T17:10:42Z")

</div>

> [@adolgert](#):
>
> Division - The modulus character, `/` , becomes div in Julia, `÷` . The `/` in Julia is floating-point division, so it will promote integers to reals.

I may be missing something here, but both julia and C have `%` as the modulus operator, don’t they? 🤔 (or rather, the remainder of division)

---

<div class="post-metadata">

### Author: ![adolgert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adolgert/32/20286_2.png) [@adolgert](https://discourse.julialang.org/u/adolgert)
#### Post date: [February 6, 2021, 5:13pm UTC](https://discourse.julialang.org/t/from-c-to-julia-notes-from-a-low-level-port/54743/3 "2021-02-06T17:13:40Z")

</div>

Ah! You’re so right. I meant “solidus,” which is the name of the forward-slash. That’s what I get for trying to be fancy. They do both have the modulus, and it means the same thing in both. Thank you!

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [February 6, 2021, 5:19pm UTC](https://discourse.julialang.org/t/from-c-to-julia-notes-from-a-low-level-port/54743/4 "2021-02-06T17:19:31Z")

</div>

You’re welcome! 🙂

There was a great article about why the operator precedence in C between `&` and `&&` is the way it is (historical reasons older than `grep`), but I can’t seem to find it anymore 😕

Edit: Found it!

> **[Hundred year mistakes](https://ericlippert.com/2020/02/27/hundred-year-mistakes/)**
>
> My manager and I got off on a tangent in our most recent one-on-one on the subject of the durability of design mistakes in programming languages. A particular favourite of mine is the worst of the …

---

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [February 6, 2021, 5:30pm UTC](https://discourse.julialang.org/t/from-c-to-julia-notes-from-a-low-level-port/54743/5 "2021-02-06T17:30:13Z")

</div>

> [@adolgert](#):
>
> In Julia, for-loops have a new scope for the loop variable.

You can have the C behavior with the `outer` keyword:

> [@How to capture a for loop index after break?](https://discourse.julialang.org/t/how-to-capture-a-for-loop-index-after-break/46342/4):
>
> Are you looking for the outer keyword? function foo(p) local i for outer i = 1:100 rand() \> p && break end return i end

---

<div class="post-metadata">

### Author: ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)
#### Post date: [February 6, 2021, 5:49pm UTC](https://discourse.julialang.org/t/from-c-to-julia-notes-from-a-low-level-port/54743/6 "2021-02-06T17:49:19Z")

</div>

Very nice! I think you might have a couple of points that aren’t made in [Noteworthy differences from C/C++](https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-C/C). It might be good to add a line or two there as hopefully that’s the place folks will end up if they’re porting between C and Julia.

It’s also worth noting that because it’s easy to make a .dll or .so file, and `ccall` is easy to use, one can test both versions with the same tests. I’ve done a little bit of this, making some algorithm in Julia, then porting it to C.

---

<div class="post-metadata">

### Author: ![adolgert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adolgert/32/20286_2.png) [@adolgert](https://discourse.julialang.org/u/adolgert)
#### Post date: [February 6, 2021, 6:34pm UTC](https://discourse.julialang.org/t/from-c-to-julia-notes-from-a-low-level-port/54743/7 "2021-02-06T18:34:48Z")

</div>

That C/C++ comparison seems complete, really, and very helpful, as is using `ccall`. And you’re porting _to C?_ Sure, there are good cases for that, aren’t there.

---

<div class="post-metadata">

### Author: ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)
#### Post date: [February 6, 2021, 6:49pm UTC](https://discourse.julialang.org/t/from-c-to-julia-notes-from-a-low-level-port/54743/8 "2021-02-06T18:49:07Z")

</div>

I’m doing simulations and designing control laws which ultimately get implemented in microcontrollers (or in some cases FPGAs), so it’s not like a general case of porting Julia to C. It would be more correct to say that I’m writing algorithms with relatively simple math and logical constructs knowing I won’t even have floating point hardware to use in the end.
