Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

logical operators in C

Name: Roberto Pastrana 2009-04-24 0:12

Hi /prog/
I need to do this exact thing, but only using logical operators

it prints "OP is a faggot" when one of the numbers is negative and the other is 0 or positive
-----------------------------------------
#include <stdio.h>

int main ()

{
    int a;
    int b;

    puts("Ingrese un valor.\n");
    scanf("%d", a)
    puts("Ingrese otro valor.\n");
    scanf("%d", b)

    if (a<0)
    {
        if (b>=0)
        {
            puts "OP is a faggot"
        }
    } else {
        if (b<0)
        {
            puts "OP is a faggot"   
        }
    }
}
-----------------------------------------

Name: Anonymous 2009-04-24 0:23

All programs at circuit level run entirely using boolean logic. Enjoy your completed program.

Name: Anonymous 2009-04-24 0:32

OP here

if a<0>b OR b<0>a
...

is doesn't take zeroes, but it's ok

thanks anyway fourchaners

Name: Anonymous 2009-04-24 1:21

if ((a < 0 && -b < 0) || (-a < 0 && b < 0))
    puts("OP is a \"GRUNNUR\"");

Name: Anonymous 2009-04-24 2:29

>>4
negating a variable and testing the complement of what you would test on the un-negated variable
0/10

Name: Anonymous 2009-04-24 3:00

#include <limits.h>

/* Assumes twos-complement representation like on every sane processor */
int isNegative(int i)
{
  return i & INT_MIN;
}

int isGEZero(int i)
{
  return !isNegative(i);
}

HTH, HAND.

Name: Anonymous 2009-04-24 3:04

#include <limits.h>

/* Assumes twos-complement representation like on every sane processor */
int isNegative(int i)
{
  return i & INT_MIN;
}

int isGEZero(int i)
{
  return !isNegative(i);
}

HTH, HAND.

Name: Anonymous 2009-04-24 4:13

>>7
Taking the bitwise and of a number and then checking whether it is zero is faster than comparing the initial number to zero? I don't think so. If this is faster, please explain.

Name: Anonymous 2009-04-24 4:36

>>8
It's slower, but only because of the extra function calls. l2eeflags.

Name: Anonymous 2009-04-24 4:36

>>8
Something tells me that the relative speed of these methods would be different on different architectures.

Name: Anonymous 2009-04-24 5:01

>>10
Don't be silly. There's only one architecture.

Name: Anonymous 2009-04-24 5:38

>>1
Hello, Roberto, you might consider using the code tags.

Name: Anonymous 2009-04-24 5:39

>>11
yeah, no one uses anything except plan 9 on ARM anymore.

Name: Anonymous 2009-04-24 5:45

>>13
What are you talking about? Most people are either using IA-32 or x64 with the Microsoft Windows OS.

Name: Anonymous 2009-04-24 15:37

>>8
original didn't specify performance goals, only the operators to use.

Name: Anonymous 2009-04-24 15:51

>>13
ARM, Fuck yeah!

Name: Anonymous 2009-04-24 16:15

On the x86 comparison with 0 tends to be optimized as a test eax,eax followed by a jnz/jz (which just checks the zero flag). Alternatively, you can check the upper bit of your dword to find out if it's negative or not, such as : test num,80000000h
then je/jne. A smart compiler can optimize such comparisons better than you anyway, why would you bother, and at the digital logic level implementation of some of these instructions may be as efficient as that test if not even better.

tl;dr: don't bother, listen to >>2

Name: Anonymous 2009-04-24 17:12

#include <stdio.h>

int main ()

{
    int a;
    int b;

    puts("Intrar un valore.\n");
    scanf("%d", a)
    puts("Intrar ancor un valore.\n");
    scanf("%d", b)

    if ((a<0 && b>=0) || (b<0))
    {
        puts "OP es un homosexual"
    }
}

Name: Anonymous 2009-04-24 17:45

Simplifying by exhaustive evaluation:

let P = a<0
let Q = b>=0
let X = (P & Q) | !Q

P Q X (P|!Q)
T T T   T
T F T   T
F T F   F
F F T   F[code]

thus

[code]if (a<0 || b<0){
        puts "OP es un homosexual"
    }

Name: Anonymous 2009-04-24 17:47

DONT USE TRUTH TABLE!!

Name: Anonymous 2009-04-24 17:48

BBCode failure

P Q X (P|!Q)
T T T   T
T F T   T
F T F   F
F F T   [b][i]T[/i][/b]


thus

if (a<0 || b<0){
        puts "OP es un homosexual"
    }

Name: Anonymous 2009-04-24 18:15

I need a coffee

Name: Anonymous 2009-04-24 18:18

Use Karnaugh maps.

Name: Anonymous 2009-04-24 18:20

>>23
I'd much rather use a Knuth map.

Name: Anonymous 2009-04-24 18:22

>>21,19
if ((a|b)<0)

Name: Anonymous 2009-04-24 18:42

>>25
1/10

Name: Anonymous 2009-04-24 18:45

>>26
Actually, he's right

a<0 => a has sign bit set
b<0 => b has sign bit set

a|b has sign bit set if either of those is true

Name: Anonymous 2009-04-24 18:47

OPTIMIZED by removing all ifs

#include <stdio.h>

int main () {
    int a, b, c;

    puts("Ingrese un valor.\n");
    scanf("%d", a)
    puts("Ingrese otro valor.\n");
    scanf("%d", b)

    c = (a<0 && b>=0) || b<0 && a>=0)
    puts c ? "OP is a faggot" : "";
}

Name: Anonymous 2009-04-24 18:52

Final Form

#include <stdio.h>

int main () {
    int a, b;

    puts("Ingrese un valor.\n");
    scanf("%d", a)
    puts("Ingrese otro valor.\n");
    scanf("%d", b)

    puts ((a|b)<0) ? "OP is a faggot" : "";
}

Name: Anonymous 2009-04-24 19:18

>>29
Fixed
#include <stdio.h>

int main () {
    int a, b;

    puts("Ingrese un valor.\n");
    scanf("%d", a)
    puts("Ingrese otro valor.\n");
    scanf("%d", b)

    puts ((a^b)<0) ? "OP is a faggot" : "";
}

Name: Anonymous 2009-04-24 20:06

98% of the programmer population uses emacs or vim. If you're in the remaining 2%, repost this on /prog/.

Name: 2% milk 2009-04-24 21:40

>>31
98% of the programmer population uses emacs or vim. If you're in the remaining 2%, repost this on /prog/.

Name: Anonymous 2009-04-24 21:54

2% of the programmer population uses notepad. If you're in the remaining 98%, repost this on /prog/.

Name: Anonymous 2009-04-25 5:57

100% of the programmers who matter use Textmate. If you are not, you can watch the screencasts.

Name: Anonymous 2009-04-25 5:59

I use Notepad++, suck my duck

Name: Anonymous 2009-04-25 7:11

I use emax, sick my dick.

Name: Anonymous 2009-04-25 7:24

>>1
So I guess it's kind of like rain on your wedding day, huh?

Name: Anonymous 2009-04-25 7:25

Fick mein Duck

Name: Anonymous 2009-04-25 21:02

Dein fick gemutten

Name: Anonymous 2009-04-26 2:08

Sick my duck.

Name: Anonymous 2009-04-26 2:33

if(a < 0 ^ b < 0) puts "OP is a faggot";

Name: Anonymous 2009-04-26 3:17

>>41
In Sepples, the bitwise exact or operator returns an integer, which can be implicitly converted to a boolean, but it is not good form to do so. In fact, you've made 3 implicit conversions. bool to int and bool to int and the resulting int to bool.

EXPERT POOR FORM IN AN EXPERT BAD LANGUAGE

Name: Anonymous 2009-04-26 3:24

>>42
You are, aware booleans in sepples are just ints right? There is no actual conversion taking place anywhere.

Name: Anonymous 2009-04-26 6:10

>>42
You are, aware floats in sepples are just bits right? There is no actual conversion taking place anywhere.

Name: Anonymous 2009-04-26 6:43

>>43,44
You, are, aware, how many fucking, commeas, you, are just, using right? There is no actual fucking point in commas taking place anywhere.

Name: Anonymous 2009-04-26 7:21

>>44
Yes there is. When you convert a float to another type the bits will get changed and shifted around to match the destination format specification, read: converted. No such thing happens for boolean->int or int->boolean, it simply remains as is.

Name: Anonymous 2009-04-26 7:34

>>46
 int i = 42;
0041138E  mov         dword ptr [i],2Ah
 bool f = i;
00411395  cmp         dword ptr [i],0
00411399  setne       al  
0041139C  mov         byte ptr [f],al

The bits of int i (eax) are changed to be either 0 or 1, before being copied to the bool f.
Heck, you could even say the integer is "converted" into a boolean.

Name: Trollbot9000 2009-07-01 8:53

Those that understand binary those who dont!

Name: ​​​​​​​​​​ 2010-10-26 2:38

Don't change these.
Name: Email:
Entire Thread Thread List