Bit Twidling HW problems.

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

Sug4rfree
Posts: 11
Joined: Sat Nov 01, 2008 8:44 pm UTC

Bit Twidling HW problems.

Postby Sug4rfree » Thu Jan 28, 2010 2:07 am UTC

Hey,

Homework problems. If you don't think you should tell me the answer then any hints would be appreciated.

Need to create is non 0 function using only ~ & ^ | + << >> and no conditionals, loops, etc.

Only 10 operators can be used also.

Thanks.

This is a coding question, not a CS question; moved. - phlip

Unparallelogram
Posts: 336
Joined: Mon Jul 28, 2008 4:16 am UTC

Re: Bit Twidling HW problems.

Postby Unparallelogram » Thu Jan 28, 2010 2:22 am UTC

I'm not a big fan of doing people's homework for them. But if you want any help, from anyone, you should at least define what you mean by a non-zero function. If you mean a function whose result is always nonzero, what if any constants are you allowed to use? What inputs do you take? Oh, and stick this sorta thing in Coding next time, not CS.

qbg
Posts: 586
Joined: Tue Dec 18, 2007 3:37 pm UTC

Re: Bit Twidling HW problems.

Postby qbg » Thu Jan 28, 2010 4:19 am UTC

Sug4rfree wrote:Hey,

Homework problems. If you don't think you should tell me the answer then any hints would be appreciated.

Need to create is non 0 function using only ~ & ^ | + << >> and no conditionals, loops, etc.

Only 10 operators can be used also.

Thanks.

What do you mean by "non 0 function"?

LikwidCirkel
Posts: 169
Joined: Thu Nov 08, 2007 8:56 pm UTC
Location: on this forum (duh)
Contact:

Re: Bit Twidling HW problems.

Postby LikwidCirkel » Sun Jan 31, 2010 2:10 am UTC

Code: Select all

bool nonZero(int arg) { return ~(arg + (~0));}

I'm sure there are many more ways.

... edited to remove subtraction sign - replaced with - 1 with +(~0)

User avatar
thoughtfully
Posts: 2253
Joined: Thu Nov 01, 2007 12:25 am UTC
Location: Minneapolis, MN
Contact:

Re: Bit Twidling HW problems.

Postby thoughtfully » Sun Jan 31, 2010 3:34 am UTC

That is a pretty poor specification. Do you have any more detail? Why not just

Code: Select all

int nonZero(int arg) { return 1; }

It might be interesting if it also states you can't use any literals:)

*evil laugh*
what about casts? Some compilers will let you off with warning if you omit them. So, I'm just using the "&" operator, that's ok, isn't it.. although its in unary form!

Code: Select all

int nonZero(int arg) { return &arg; }
Last edited by thoughtfully on Sun Jan 31, 2010 3:49 am UTC, edited 1 time in total.
Image
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery

stephentyrone
Posts: 778
Joined: Mon Aug 11, 2008 10:58 pm UTC
Location: Palo Alto, CA

Re: Bit Twidling HW problems.

Postby stephentyrone » Sun Jan 31, 2010 3:45 am UTC

If we're going to do people's homework for them, do it without using literals. A hint: use | and ~.
Spoiler:

Code: Select all

return x | ~x;
GENERATION -16 + 31i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 10 guests