Bit level coding?

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 2208 Views - Last Post: 04 March 2012 - 06:34 PM Rate Topic: -----

#1 killermunk1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 05-February 12

Bit level coding?

Posted 04 March 2012 - 12:21 PM

I am in the first week of my class on computer systems. I am having some issues with how to do my work. The problem in my book states "Write C expressions that evaluate to 1 when the following conditions are true, and to 0 when they are false. Assume x is of type int." The for the part that I am working on I need to set it up for Any bit of x equals to 1. Also I cannot use conditionals, loops, switch, functions, macro invocations, division, modulus, multiplication, relative comparison operators (including but not limited to == and !=) or casting.

The problem that I am having is the book does not really show any code examples( at least that I have found) about on how to do this. How would I set up something like this. I am not sure on how to even start this. Does anyone have any ideas that can help me figure out where to start?

Is This A Good Question/Topic? 0
  • +

Replies To: Bit level coding?

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,528
  • Joined: 08-August 08

Re: Bit level coding?

Posted 04 March 2012 - 12:32 PM

I'm not sure I follow you, but if x is an integer greater than 0 then it will return true.
if(x) {
  // do something
}

Was This Post Helpful? 0
  • +
  • -

#3 killermunk1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 05-February 12

Re: Bit level coding?

Posted 04 March 2012 - 12:39 PM

That was what I was thinking. With what I have learned from my previous classes this is what I thought would be a good course of action because if x was not zero it would have at lease one 1 in the bits. However I cannot is if statements in the problem.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,528
  • Joined: 08-August 08

Re: Bit level coding?

Posted 04 March 2012 - 12:49 PM

Can you use a ternary?
x ? dosomething(); : dosomethingelse();

What are the following conditions?

Quote

when the following conditions are true

Was This Post Helpful? 0
  • +
  • -

#5 killermunk1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 05-February 12

Re: Bit level coding?

Posted 04 March 2012 - 12:58 PM

I did not know about the ternary, however I cannot use it either.

The condition is if Any bit of x equals to 1. This is the one I am currently working on( there is also Any bit of x equals 0, Any bit in the least significant byte of x equals 1 and Any bit in the most significant byte of x equals 0).
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,528
  • Joined: 08-August 08

Re: Bit level coding?

Posted 04 March 2012 - 01:32 PM

But how are you allowed to test it and what changes if it's true? For example, you could do:
y = (x | 0) * z;

and y would be set to x*z if x > 0 but this assumes that y and z are numeric.
Oops. That would include multiplicaton.

This post has been edited by CTphpnwb: 04 March 2012 - 01:35 PM

Was This Post Helpful? 0
  • +
  • -

#7 killermunk1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 05-February 12

Re: Bit level coding?

Posted 04 March 2012 - 01:53 PM

I see you are running in to the same problem that I am everything I can think to try is against the rules. I sent an e-mail to the teacher for clarification earlier I am hoping that he get back to me because this is a bit annoying. Also on a side note I see you edited your post how does one do that?
Was This Post Helpful? 0
  • +
  • -

#8 killermunk1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 05-February 12

Re: Bit level coding?

Posted 04 March 2012 - 02:05 PM

This is what it says I can do.


■ All bit-level and logic operations.

■ Left and right shifts, but only with shift amounts between 0 and w − 1.

■ Addition and subtraction.

■ Integer constants INT_MIN and INT_MAX.
Was This Post Helpful? 0
  • +
  • -

#9 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 195
  • View blog
  • Posts: 544
  • Joined: 04-August 09

Re: Bit level coding?

Posted 04 March 2012 - 02:11 PM

You could do something like this:
#include <stdio.h>

int main()
{
    int x;
    scanf("%d",&x);
    x && printf("At least one bit is set to 1\n");
    !x && printf("All bits are 0\n");
    return 0;
}



edit:

I Misread your question the expression
x && 1
satisfies your requirements if I've understood them correctly. It will evaluate to 0 if x is 0 and 1 otherwise.

This post has been edited by simeesta: 04 March 2012 - 02:21 PM

Was This Post Helpful? 1
  • +
  • -

#10 killermunk1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 05-February 12

Re: Bit level coding?

Posted 04 March 2012 - 02:21 PM

View Postsimeesta, on 04 March 2012 - 02:11 PM, said:

You could do something like this:
#include <stdio.h>

int main()
{
    int x;
    scanf("%d",&x);
    x && printf("At least one bit is set to 1\n");
    !x && printf("All bits are 0\n");
    return 0;
}



This seems to work well, thank you. This does make sense(although I am not familiar with stdio.h). Can you explain what all is happening in the code.
Was This Post Helpful? 0
  • +
  • -

#11 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 195
  • View blog
  • Posts: 544
  • Joined: 04-August 09

Re: Bit level coding?

Posted 04 March 2012 - 02:27 PM

The stdio.h bit just allows me to call the printf function (as well as other things).

x && printf("At least one bit is set to 1\n");

It evaluates x. If x is false it knows the statement will be false (as false && anything is false) so it doesnt call the printf - theres no need) but if x is true then printf needs to be called to evaluate the expression (as true && something evaluates to something.)

Similarly for the other line.
Was This Post Helpful? 0
  • +
  • -

#12 J-e-L-L-o  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 204
  • Joined: 23-January 11

Re: Bit level coding?

Posted 04 March 2012 - 02:28 PM

I think you are over thinking it. If x is an integer, then write a few statements that are true and some are false. Sounds like one of those questions at the end of the chapter to make sure you comprehend.

So X is an integer. Write a true statement, and a false statement using logic operations

int x

x = 23 > 12
check bit (will be 1) or C will return true

x = NOT 23 > 12 (can't remember if you can do this in C, but you can in C++)
or
x = 23 < 12
(will return false)
Was This Post Helpful? 0
  • +
  • -

#13 killermunk1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 05-February 12

Re: Bit level coding?

Posted 04 March 2012 - 02:32 PM

That makes sense. I a rather new to programming so bare with me. How does the program know that there is an 1 bit in x?

To jello, I might be over thinking although I cannot use <,<=,>,>=,== or !=

This post has been edited by killermunk1: 04 March 2012 - 02:36 PM

Was This Post Helpful? 0
  • +
  • -

#14 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 195
  • View blog
  • Posts: 544
  • Joined: 04-August 09

Re: Bit level coding?

Posted 04 March 2012 - 02:37 PM

In C 0 is false and any other value is true (boolean expressions tend to return 1 as true though.) Generally 0 is represented in binary by 32 zero's (if ints are 32 bits in size), any other value will have at least one bit set to 1 (otherwise it would equal zero).

But you can use logical operators like &&. so x && 1 which evaluates to whether x is non-zero.

This post has been edited by simeesta: 04 March 2012 - 02:39 PM

Was This Post Helpful? 0
  • +
  • -

#15 killermunk1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 05-February 12

Re: Bit level coding?

Posted 04 March 2012 - 02:45 PM

View Postsimeesta, on 04 March 2012 - 02:37 PM, said:

In C 0 is false and any other value is true (boolean expressions tend to return 1 as true though.) Generally 0 is represented in binary by 32 zero's (if ints are 32 bits in size), any other value will have at least one bit set to 1 (otherwise it would equal zero).

But you can use logical operators like &&. so x && 1 which evaluates to whether x is non-zero.


Now I am thinking about the second one "Any bit of x equals 0". From what I am thinking this would be much the opposite of the first problem where anything would have a 1 as long as it is not zero. This one I would assume everything would have a zero in it with the exception of the highest possible number. This one I am thinking I will need to check the bits manually but not sure how to go about that.

This post has been edited by killermunk1: 04 March 2012 - 02:46 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2