Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Programming in C

Name: Anonymous 2012-01-15 23:50

sup /prog/,

I'm coding in C. I have an char array that looks like this: [0,1,1,0,0,0,1,1].

How can I write this into a binary or single char so that it'll be a single variable, i.e. b = 01100011?

And then convert that back into decimal?

Name: Anonymous 2012-01-16 0:41

>>10

[q]"You shouldn't add it. You should or it?" [/q]

Consider the following:

char b = 0;
char arr[8] = {0,1,1,0,0,0,1,1};


[q]b += arr[1];[/code]
Adding 1 to b every time will just add 1 and 0 to the number. So at the end you'll end up with something like 6 or 8, when in fact arr represents 99 in binary.

[q]b |= (arr[1] << 6);[/q]

arr = 0 1 1 0 0 0 1 1
        |
        V
b   = 0 1 0 0 0 0 0 0


With OR the bit is set in the location it needs to be set at.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List