Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Buddy Bell
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21128
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always thought the principal idea when writing C code was to use the fewest keystrokes possible
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bringing in another language point: in JavaScript !! is an often-used idiom to convert a value into a Boolean.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote: in JavaScript !! to convert a value into a Boolean.


So not not foo is boolean?
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic