J
J
jack2392016-01-27 22:43:38
Python
jack239, 2016-01-27 22:43:38

Rewriting code from python to c. One line is not clear at all, how?

It is not clear how to write the line byte |= bit << bit_i in C
Here is the full code:

f = open(outfn, 'wb')
    f.write(struct.pack('HH', H, W))
    for j in range(H):
        for i in range(0, W, 8):
            byte = 0
            for bit_i in range(8):
                try:
                    bit = im.getpixel((i + bit_i, j)) < 255
                except:
                    bit = False
                # sys.stdout.write(' X'[bit])
                byte |= bit << bit_i
            f.write(struct.pack('B', byte))

    print 'wrote', outfn

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2016-01-27
@Rsa97

It will be exactly the same
byte |= bit << bit_i;

S
sim3x, 2016-01-27
@sim3x

https://wiki.python.org/moin/BitwiseOperators
is equivalent
byte = byte | foo

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question