27 Replies - 2208 Views - Last Post: 04 March 2012 - 06:34 PM
#1
Bit level coding?
Posted 04 March 2012 - 12:21 PM
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?
Replies To: Bit level coding?
#2
Re: Bit level coding?
Posted 04 March 2012 - 12:32 PM
if(x) {
// do something
}
#3
Re: Bit level coding?
Posted 04 March 2012 - 12:39 PM
#4
Re: Bit level coding?
Posted 04 March 2012 - 12:49 PM
x ? dosomething(); : dosomethingelse();
What are the following conditions?
Quote
#5
Re: Bit level coding?
Posted 04 March 2012 - 12:58 PM
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).
#6
Re: Bit level coding?
Posted 04 March 2012 - 01:32 PM
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
#7
Re: Bit level coding?
Posted 04 March 2012 - 01:53 PM
#8
Re: Bit level coding?
Posted 04 March 2012 - 02:05 PM
■ 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.
#9
Re: Bit level coding?
Posted 04 March 2012 - 02:11 PM
#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 && 1satisfies 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
#10
Re: Bit level coding?
Posted 04 March 2012 - 02:21 PM
simeesta, on 04 March 2012 - 02:11 PM, said:
#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.
#11
Re: Bit level coding?
Posted 04 March 2012 - 02:27 PM
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.
#12
Re: Bit level coding?
Posted 04 March 2012 - 02:28 PM
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)
#13
Re: Bit level coding?
Posted 04 March 2012 - 02:32 PM
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
#14
Re: Bit level coding?
Posted 04 March 2012 - 02:37 PM
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
#15
Re: Bit level coding?
Posted 04 March 2012 - 02:45 PM
simeesta, on 04 March 2012 - 02:37 PM, said:
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
|
|

New Topic/Question
Reply



MultiQuote





|