Page 1 of 2
BITWISE challenge
Posted: Thu Nov 13, 2008 10:58 am
by avansc
this challenge is so easy that people overlook it, and the simple solution never gets seen.
your challenge is to write a program or function if you like that will take a integer number and will convert it if need be so it first in a channel.
for example lets say 0'a and 1's have to be in channel 1, 2' and 3' have to be in channel 1. (this is called the ceil channel)
you can also say 0's and 1's go in channel 0, and so on, that's called the floor channel.
(id suggest trying the ciel channel way, its a little easier, but only by like a minus sign.)
anyways, there are literally hundreds of solutions. lets see what you guys can come up with. (ps: the title of the challenge is a huge hint.)
sample input and output.
input = output
floor ceil
0 = 0 0 = 1
1 = 0 1 = 1
2 = 2 2 = 3
3 = 2 3 = 3
4 = 4 4 = 5
5 = 4 5 = 5
6 = 6 6 = 7
7 = 6 7 = 7
8 = 8 8 = 9
9 = 8 9 = 9
Re: BITWISE challenge
Posted: Thu Nov 13, 2008 11:01 am
by MarauderIIC
So what's a channel?
Re: BITWISE challenge
Posted: Thu Nov 13, 2008 11:12 am
by avansc
MarauderIIC wrote:So what's a channel?
its basically just the word i used to discribe filtering numbers into "channel"
think of it this way. 0 and 1 are small enough to fit into pipe number one, 2 and 3 into pipe number 2 and so on. in (X and X+1 can fit into Pipe X)
how would you write a function that would take a number and figeu out what pipe it needs to go in.
Re: BITWISE challenge
Posted: Thu Nov 13, 2008 12:14 pm
by Amarant
Is the channel supposed to have a variable width?
Re: BITWISE challenge
Posted: Thu Nov 13, 2008 12:21 pm
by avansc
Amarant wrote:Is the channel supposed to have a variable width?
no for this challenge we will only make that its 2 number {0,1} = channel 1 {2,3} = channel 2 and so on.
Re: BITWISE challenge
Posted: Thu Nov 13, 2008 12:37 pm
by avansc
maybe i explained this badly.
just write a function that produces these ouputs for these inputs.
f(0) = 1
f(1) = 1
f(2) = 3
f(3) = 3
f(4) = 5
f(5) = 5
...
...
..
f(10) = 11
F(11) = 11
Re: BITWISE challenge
Posted: Thu Nov 13, 2008 12:42 pm
by Amarant
Like this?
Code: Select all
int math_floor(int value)
{
return value & ~1;
}
int math_ceil(int value)
{
return (value & ~1) + 1;
}
Re: BITWISE challenge
Posted: Thu Nov 13, 2008 12:52 pm
by avansc
Amarant wrote:Like this?
Code: Select all
int math_floor(int value)
{
return value & ~1;
}
int math_ceil(int value)
{
return (value & ~1) + 1;
}
very nice, here is another one a|a^1
do you know what the "~" (tilde) opperand does?
Re: BITWISE challenge
Posted: Thu Nov 13, 2008 1:04 pm
by Amarant
It's the 'One's Complement Operator' and it flips all the bits in the integer, not sure what happens with negative numbers though.
So I wrote a little program to find out what is does with negative numbers. And it really does flip every bit:
Code: Select all
one's complement of -3: ~11111111111111111111111111111101 = 00000000000000000000000000000010
one's complement of -2: ~11111111111111111111111111111110 = 00000000000000000000000000000001
one's complement of -1: ~11111111111111111111111111111111 = 00000000000000000000000000000000
one's complement of 0: ~00000000000000000000000000000000 = 11111111111111111111111111111111
one's complement of 1: ~00000000000000000000000000000001 = 11111111111111111111111111111110
one's complement of 2: ~00000000000000000000000000000010 = 11111111111111111111111111111101
As you can see my floor function just does a bitwise and with -2.
I did a compile and looked at the generated assembly, it looked like this:
Re: BITWISE challenge
Posted: Fri Nov 14, 2008 9:55 am
by avansc
Amarant wrote:It's the 'One's Complement Operator' and it flips all the bits in the integer, not sure what happens with negative numbers though.
So I wrote a little program to find out what is does with negative numbers. And it really does flip every bit:
Code: Select all
one's complement of -3: ~11111111111111111111111111111101 = 00000000000000000000000000000010
one's complement of -2: ~11111111111111111111111111111110 = 00000000000000000000000000000001
one's complement of -1: ~11111111111111111111111111111111 = 00000000000000000000000000000000
one's complement of 0: ~00000000000000000000000000000000 = 11111111111111111111111111111111
one's complement of 1: ~00000000000000000000000000000001 = 11111111111111111111111111111110
one's complement of 2: ~00000000000000000000000000000010 = 11111111111111111111111111111101
As you can see my floor function just does a bitwise and with -2.
I did a compile and looked at the generated assembly, it looked like this:
did you know that it fliped the bits before you wrote the program.
and do you know what 2's compliment it?
how about little and big endian?
these are all very interesting fundamental concepts of CS
Re: BITWISE challenge
Posted: Fri Nov 14, 2008 10:29 am
by MarauderIIC
I didn't think they were that interesting since I don't really remember them =) Maybe it was just the way they were presented to me.
Re: BITWISE challenge
Posted: Fri Nov 14, 2008 10:40 am
by Falco Girgis
They're extremely relevant in computer engineering when you're working at super low levels with assembly and hardware. Maybe not so much for higher level computer sciencey and stuff.
Re: BITWISE challenge
Posted: Fri Nov 14, 2008 11:58 am
by avansc
GyroVorbis wrote:They're extremely relevant in computer engineering when you're working at super low levels with assembly and hardware. Maybe not so much for higher level computer sciencey and stuff.
well maybe not for the avarage developer, but someone who is concerned about efficiency i think its pivotal.
also, its very nice to have a good grasp on these things when your program is giving you numbers that you think is wrong.
Re: BITWISE challenge
Posted: Fri Nov 14, 2008 12:48 pm
by Amarant
avansc wrote:
did you know that it fliped the bits before you wrote the program.
and do you know what 2's compliment it?
how about little and big endian?
these are all very interesting fundamental concepts of CS
Yes, I didn't just randomly try operators to see which one worked.
2's complement is the way negative numbers are represented in signed types like char, int etc.. (not float or double though).
Endianness is the way the bytes are ordered in memory for multibyte types. I believe the term endianness is also applicable to bit ordering within bytes.
Re: BITWISE challenge
Posted: Fri Nov 14, 2008 1:04 pm
by avansc
Amarant wrote:avansc wrote:
did you know that it fliped the bits before you wrote the program.
and do you know what 2's compliment it?
how about little and big endian?
these are all very interesting fundamental concepts of CS
Yes, I didn't just randomly try operators to see which one worked.
2's complement is the way negative numbers are represented in signed types like char, int etc.. (not float or double though).
Endianness is the way the bytes are ordered in memory for multibyte types. I believe the term endianness is also applicable to bit ordering within bytes.
yeah, i dont know why but i find the history of all this stuff pretty interesting. maybe im just weird.