It's not a secret anymore!*
The moose likes Java in General and the fly likes portin to java from c - can't figure !!bit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "portin to java from c - can Watch "portin to java from c - can New topic
Author

portin to java from c - can't figure !!bit

Buddy Bell
Greenhorn

Joined: Feb 09, 2013
Posts: 1
I am porting portions of gmfsk written in c to java and it works sometimes. In trying to get the bugs out I need to nail down what the !! operator does in c. Googleing does not help me, it's like no one has seen it before. The actual segment of code is:



I had gotten around it in java by saying:


but I don't have a clue if that is right and it may be the source of the unreliable results I am getting.

Any help resolving this mystery operator would be appreciated. Thanks.

Buddy
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
Welcome to the Ranch

I have added code tags to your post, which makes it look a lot better.
I have only ever come across a bang‑bang operator in Eiffel, and I believe it has been removed from the language. I don’t think there is a bang‑bang operator in C, but there is a bang operator. You can usually use the same operator twice. I shall investigate whether it cancels itself out.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
Does that help?

It would appear the bang operator turns a true to false and vice versa. In Java you would have to mimic that with !(i == 0), !(i != 0), etc. Of course those reduce to i != 0 and i == 0, which are more elegant than bang‑bang.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

The use of !! I've seen - which is backed up by Campbell's details - is to "normalise" a value that represents a boolean. You can have two values that are both considered "true", but are not equal. But once you've used !! they have the same value that still represents "true". I've seen it more often in dynamic languages where you have different types that can be treated as booleans. E.g. in PHP all of 0, "", null and an empty array are treated as "false" - using !! is a convenient way to force them to false.

In this case, I think you could replace it with (bit == 0) ? 0 : 1
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Matthew Brown wrote:(bit == 0) ? 0 : 1


Personally, I'd much prefer to use the keywords.
(bit == 0) ? false : true


There is a common, but very hard to find bug that can occur if you mistype @matt's code and have it look like:

(bit == 0) ? 1 : 0



This can be a real devil to debug. So I always use the keywords.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Pat Farrell wrote:Personally, I'd much prefer to use the keywords.
(bit == 0) ? false : true


Which is equivalent to simply bit != 0. It's true that your version is more readable, but on the other hand readability doesn't seem to have been one of the major concerns when the original C code was written.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
I always thought the principal idea when writing C code was to use the fewest keystrokes possible
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Pat Farrell wrote:Personally, I'd much prefer to use the keywords.
(bit == 0) ? false : true

I'm with you Pat; particularly as C's interpretation of ints as boolean results runs contrary to one of its major uses: method "success". Which is why you end up with stuff like:
if (!strcmp(str1, str2)) ...

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Campbell Ritchie wrote:I always thought the principal idea when writing C code was to use the fewest keystrokes possible


Sadly, this is true too often.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

Bringing in another language point: in JavaScript !! is an often-used idiom to convert a value into a Boolean.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Bear Bibeault wrote: in JavaScript !! to convert a value into a Boolean.


So not not foo is boolean?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Winston Gutkowski wrote:
Pat Farrell wrote:Personally, I'd much prefer to use the keywords.
(bit == 0) ? false : true

I'm with you Pat; particularly as C's interpretation of ints as boolean results runs contrary to one of its major uses: method "success". Which is why you end up with stuff like:
if (!strcmp(str1, str2)) ...

Winston

That's not entirely the same. strcmp is a function that returns something similar to the Comparator.compare method. This !strcmp(str1, str2) merely means that "if str1 and str2 are not the same". Because in C, any non-0 numeric value is true and 0 is false, as expected.

There are some functions that, indeed, use 0 as success and non-zero as a specific error code. The Windows and UNIX APIs are full of those calls.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Rob Spoor wrote:That's not entirely the same. strcmp is a function that returns something similar to the Comparator.compare method. This !strcmp(str1, str2) merely means that "if str1 and str2 are not the same"...

No, it means "if str1 and str2 are the same"; which is my whole point.

In almost every case that I know of, 0 as a return code means "success" (and in the case of strcmp() it means "equal"), but 0 == false; so you're always dealing with "inverted" logic.

That's why I like booleans.

Winston
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

Pat Farrell wrote:So not not foo is boolean?

Yes. One bang coerces the value to a Boolean, but inverses the value in the process. The second bang reverses the inversion resulting in the Boolean equivalent of the original value. In JavaScript, everything but 0, null, or undefined is "truth"; that is, coerces to true.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

Pat Farrell wrote:So not not foo is boolean?

Yes. One bang coerces the value to a Boolean, but inverses the value in the process. The second bang reverses the inversion resulting in the Boolean equivalent of the original value. In JavaScript, everything but 0, null, or undefined is "truthy"; that is, coerces to true.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Winston Gutkowski wrote:
Rob Spoor wrote:That's not entirely the same. strcmp is a function that returns something similar to the Comparator.compare method. This !strcmp(str1, str2) merely means that "if str1 and str2 are not the same"...

No, it means "if str1 and str2 are the same"; which is my whole point.

In almost every case that I know of, 0 as a return code means "success" (and in the case of strcmp() it means "equal"), but 0 == false; so you're always dealing with "inverted" logic.

That's why I like booleans.

Winston

Ah right, ! turns the 0 (false) into true. How could I overlook that...

However, for functions like strcmp, there is no such thing as "success" or "failure". 0 is just as valid a return value as any other number, they just mean different things. In this case the problem is turning something that isn't a boolean (a proper numeric value) into a boolean.
 
GeeCON Prague 2014
 
subject: portin to java from c - can't figure !!bit