1
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?
3
Name:
Anonymous
2012-01-15 23:54
How C is better than Haskell?
Lisp | Haskell
----------------------------|---------------------------------------------------
reduce | fold, foldl, foldr, foldl',
| foldr1, foldl1, foldl1...
----------------------------|---------------------------------------------------
map | map, mapM, mapM_, mapAccumL, mapAccumR, mapAccum,
| mapAndUnzipM, mempty, mappend, mapAccumRWithKey,
| mapAccumWithKey, mapMaybe, mapMaybeT mapEither,
| mapEitherWithKey, mapMaybeWithKey, mapFst, mapSnd,
| mapIndices, mapArray, mapListT, mapMonotonic,
| concatMap, amap, ixmap...
----------------------------|---------------------------------------------------
elt | fst snd thd fth ffth...
4
Name:
Anonymous
2012-01-16 0:06
>>1
You should use bitwise shift operators.
Initialize an unsigned variable to 0. Then in a loop bitwise or with the array's element and then shift.
5
Name:
Anonymous
2012-01-16 0:09
n = 0;
for (i = 0; i < 8; i++)
n |= array[i] << i;
for (i = 0; i < 8; i++)
array[i] = n & (1 << i) ? 1 : 0;
6
Name:
Anonymous
2012-01-16 0:12
>>4
Thanks. Don't think I'm getting it though.. I have:
unsigned char p = 0;
for(int n = 0; n < (sizeof array); n++) {
p+=array[n];
p << 1;
}
But this didn't return what I wanted. What am I getting wrong?
7
Name:
Anonymous
2012-01-16 0:17
>>6
You aren't using Haskell.
8
Name:
Anonymous
2012-01-16 0:21
>>6
>>5 got the solution for you. You shouldn't add it. You should or it. Take a pencil and write them down. You will see the difference.
Btw, you should start from 7 and loop through 0. Don't forget that.
9
Name:
Anonymous
2012-01-16 0:24
void Array2Char(char* arr, char* dst)
{
(*dst) |= (arr[0] << 7);
(*dst) |= (arr[1] << 6);
(*dst) |= (arr[2] << 5);
(*dst) |= (arr[3] << 4);
(*dst) |= (arr[4] << 3);
(*dst) |= (arr[5] << 2);
(*dst) |= (arr[6] << 1);
(*dst) |= (arr[7]);
}
10
Name:
Anonymous
2012-01-16 0:24
>>8
Thanks.
>>5 worked but produced the wrong outputs. (01101111 went to 246 for example)
Can you explain more what you mean by "You shouldn't add it. You should or it?"
11
Name:
Anonymous
2012-01-16 0:33
void Char2Array(char* arr, char src)
{
arr[0] = (src & (1 << 7)) ? 1 : 0;
arr[1] = (src & (1 << 6)) ? 1 : 0;
arr[2] = (src & (1 << 5)) ? 1 : 0;
arr[3] = (src & (1 << 4)) ? 1 : 0;
arr[4] = (src & (1 << 3)) ? 1 : 0;
arr[5] = (src & (1 << 2)) ? 1 : 0;
arr[6] = (src & (1 << 1)) ? 1 : 0;
arr[7] = (src & 1 ) ? 1 : 0;
}
12
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.
13
Name:
Anonymous
2012-01-16 0:46
Thanks for the help guys.
>>5 's solution actually flipped the bit sequence but I figured it out.
14
Name:
Anonymous
2012-01-16 1:02
bitfield struct
typedef struct {
unsigned char bit8 :1;
unsigned char bit7 :1;
unsigned char bit6 :1;
unsigned char bit5 :1;
unsigned char bit4 :1;
unsigned char bit3 :1;
unsigned char bit2 :1;
unsigned char bit1 :1;
} divisibleByte;
15
Name:
Anonymous
2012-01-16 1:27
>>14
Hey, that's actually a pretty good idea!
int main()
{
divisibleByte a;
a.bit8 = a.bit7 = a.bit6 = a.bit5 = a.bit4 = a.bit3 = a.bit2 = a.bit1 = 1;
printf("%d\n", *((unsigned char*)&a));
a.bit8 = 0;
printf("%d\n", *((unsigned char*)&a));
return 0;
}
With GCC with no -OX flag a lot of ORs and lea is generated. On -O4 it's just something like
mov edx,0xff.
17
Name:
Anonymous
2012-01-16 2:50
>>12
Quote by putting a greater-than symbol and a space before a line
Result looks like this
For inline quotes the custom is to use ``faggot quotes''.