BITWISE challenge
Moderator: Coders of Rage
BITWISE challenge
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
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
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: BITWISE challenge
So what's a channel?
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Re: BITWISE challenge
its basically just the word i used to discribe filtering numbers into "channel"MarauderIIC wrote:So what's a 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: BITWISE challenge
no for this challenge we will only make that its 2 number {0,1} = channel 1 {2,3} = channel 2 and so on.Amarant wrote:Is the channel supposed to have a variable width?
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: BITWISE challenge
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
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
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: BITWISE challenge
Like this?
Code: Select all
int math_floor(int value)
{
return value & ~1;
}
int math_ceil(int value)
{
return (value & ~1) + 1;
}
177
Re: BITWISE challenge
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?
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: BITWISE challenge
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:
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:
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
I did a compile and looked at the generated assembly, it looked like this:
Code: Select all
andl $-2, %eax
177
Re: BITWISE challenge
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:As you can see my floor function just does a bitwise and with -2.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
I did a compile and looked at the generated assembly, it looked like this:Code: Select all
andl $-2, %eax
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
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: BITWISE challenge
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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: BITWISE challenge
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
well maybe not for the avarage developer, but someone who is concerned about efficiency i think its pivotal.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.
also, its very nice to have a good grasp on these things when your program is giving you numbers that you think is wrong.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: BITWISE challenge
Yes, I didn't just randomly try operators to see which one worked.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
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.
177
Re: BITWISE challenge
yeah, i dont know why but i find the history of all this stuff pretty interesting. maybe im just weird.Amarant wrote:Yes, I didn't just randomly try operators to see which one worked.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
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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"