BITWISE challenge

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

BITWISE challenge

Post 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
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: BITWISE challenge

Post by MarauderIIC »

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.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: BITWISE challenge

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Amarant
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Wed Nov 05, 2008 9:52 am

Re: BITWISE challenge

Post by Amarant »

Is the channel supposed to have a variable width?
177
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: BITWISE challenge

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: BITWISE challenge

Post 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
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Amarant
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Wed Nov 05, 2008 9:52 am

Re: BITWISE challenge

Post by Amarant »

Like this?

Code: Select all

int math_floor(int value)
{
    return value & ~1;
}

int math_ceil(int value)
{
    return (value & ~1) + 1;
}
177
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: BITWISE challenge

Post 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?
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Amarant
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Wed Nov 05, 2008 9:52 am

Re: BITWISE challenge

Post 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:

Code: Select all

	andl	$-2, %eax
177
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: BITWISE challenge

Post 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:

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"
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: BITWISE challenge

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Falco Girgis
Elysian Shadows Team
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

Post 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.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: BITWISE challenge

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Amarant
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Wed Nov 05, 2008 9:52 am

Re: BITWISE challenge

Post 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.
177
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: BITWISE challenge

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Post Reply