New C homework. really confused

Danimal1209

Senior member
Nov 9, 2011
355
0
0
My objective here is to write functions that perform bitwise binary and unary operations. I just need to get an idea of how I can perform these, I really have no idea.

For example, I need to write a bitwise OR of x and y. I know in code it would be set like this:

int randomVar = x | y

But, I cannot do this. I'm not allowed to use the | operator. I'm only allowed to use the NOT(~) and AND(&) operators. And I cannot use any kind of loop either. I'm really confused with this.

Any help? I would really not prefer an actual answer, but more of a prod in the right direction.
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
Think about it like a hardware problem. You want an OR gate, but you only have AND and NOT gates. How can you wire them to make something that behaves like an OR gate?

I think the fact that you're doing this in software is making it more complicated than it should be. You don't need loops, functions, etc. Just think about gates.
 

LumbergTech

Diamond Member
Sep 15, 2005
3,622
1
0
Think about it like a hardware problem. You want an OR gate, but you only have AND and NOT gates. How can you wire them to make something that behaves like an OR gate?

I think the fact that you're doing this in software is making it more complicated than it should be. You don't need loops, functions, etc. Just think about gates.

might be equally confusing if they havent taken an introductory hardware course
 

Danimal1209

Senior member
Nov 9, 2011
355
0
0
I figured it out. I think it was to not both variables, then AND them.

I had a whole different thought process going on in my head. I was totally over thinking it.

Thanks guys! I'll probably have more C questions later lol.
 

mv2devnull

Golden Member
Apr 13, 2010
1,539
169
106
More? Boolean logic is not very language-specific, so lets rephrase: you might have a C question later.
 

Fallen Kell

Diamond Member
Oct 9, 1999
6,249
561
126
I think this question actually raises a fundamental issue more than anything else. I have seen more and more schools simply not having/requiring boolean logic courses anymore. Even my school did not require it (at least for computer science majors).

I had it because I started as an Electrical and Computer Engineer and then changed majors to Computer Science. And while so many of my classmates were struggling in a few of the classes my friend (who also changed majors at the same time) and I found the concepts very easy because we had taken a logic course.
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
I think this question actually raises a fundamental issue more than anything else. I have seen more and more schools simply not having/requiring boolean logic courses anymore. Even my school did not require it (at least for computer science majors).

I had it because I started as an Electrical and Computer Engineer and then changed majors to Computer Science. And while so many of my classmates were struggling in a few of the classes my friend (who also changed majors at the same time) and I found the concepts very easy because we had taken a logic course.

I double majored in CS and EE, so I got to see both sides of it.

As part of the CS curriculum, the CS students had to take the EE digital logic design class. It was practically a blow off class for the EE students, but the CS students really struggled with it.

On the other hand, the EE students had to take a simple programming course (basically covering the first few weeks of an intro to CS course) and they really struggled with it.

There must be some sort of mindset difference between the two majors.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
I double majored in CS and EE, so I got to see both sides of it.

As part of the CS curriculum, the CS students had to take the EE digital logic design class. It was practically a blow off class for the EE students, but the CS students really struggled with it.

On the other hand, the EE students had to take a simple programming course (basically covering the first few weeks of an intro to CS course) and they really struggled with it.

There must be some sort of mindset difference between the two majors.

There was some type difference when I went to school (in the dark ages) there was no CS department. Computers were almost non existent - think slide rules.

I was an EE and struggled badly in electronics courses. Programming courses; compiles, languages, applications were a piece of cake. My best man was the reverse. He could work with circuits in his sleep; but for the life of him, not be able to analyze a problem and break it apart into components.
 

sourceninja

Diamond Member
Mar 8, 2005
8,805
65
91
I find minecraft is a neat way to teach people logic. My wife is taking a intro programming course and I used minecraft to help her get the basics of logic gates.
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
There was some type difference when I went to school (in the dark ages) there was no CS department. Computers were almost non existent - think slide rules.

I was an EE and struggled badly in electronics courses. Programming courses; compiles, languages, applications were a piece of cake. My best man was the reverse. He could work with circuits in his sleep; but for the life of him, not be able to analyze a problem and break it apart into components.

I found analog electronics to be really difficult and I hated the math. I'm pretty good at software and digital electronics, which require the same logic mindset.
 

Maximilian

Lifer
Feb 8, 2004
12,604
15
81
int randomVar = ~x & y

NOT x and Y, is the the answer you got OP?

I dont know C im currently learning java but i remember the boolean algebra module a few months back being a real pita o_O