bitwise and

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2231 Views - Last Post: 30 March 2013 - 01:45 PM Rate Topic: -----

#1 CY5   User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 413
  • Joined: 28-September 12

bitwise and

Posted 29 March 2013 - 04:48 AM

As i was going through code
// Display font info.
import java.applet.*;
import java.awt.*;
/*
<applet code="FontInfo" width=350 height=60>
</applet>
*/
public class FontInfo extends Applet {
public void paint(Graphics g) {
Font f = g.getFont();
String fontName = f.getName();
String fontFamily = f.getFamily();
int fontSize = f.getSize();
int fontStyle = f.getStyle();
String msg = "Family: " + fontName;
msg += ", Font: " + fontFamily;
msg += ", Size: " + fontSize + ", Style: ";
if((fontStyle & Font.BOLD) == Font.BOLD)
msg += "Bold ";
if((fontStyle & Font.ITALIC) == Font.ITALIC)
msg += "Italic ";
if((fontStyle & Font.PLAIN) == Font.PLAIN)
msg += "Plain ";
g.drawString(msg, 4, 16);
}
}


i got confused on the use of bitwise and operator,program is correct but i don't understand its working like
if((fontStyle & Font.ITALIC) == Font.ITALIC)


what this mean, fontsyle contain information about style of text and font.italic is also a style of text but what Bitwise And operator doing in between,i know working of it like for eg: 1&4=0,but what is (font.italic & font.italic),Suppose if my text style is italic

Is This A Good Question/Topic? 0
  • +

Replies To: bitwise and

#2 Flukeshot   User is offline

  • A little too OCD
  • member icon

Reputation: 418
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: bitwise and

Posted 29 March 2013 - 05:05 AM

From a beginner's perspective the bitwise comparison operators are different from "regular" comparison operators in one important function.

if(clause1 &  clause2)//checks clause 2 regardless of the result of clause1.
if(clause1 && clause2)//checks clause 2 only if the result of clause1 is true.

if(clause1 |  clause2)//checks clause 2 regardless of the result of clause1.
if(clause1 || clause2)//checks clause 2 only if the result of clause1 is false.


The reason for this is that if a single clause in an 'AND gate'(&&) is false, the result will be false, similarly for an 'OR gate'(||) is a single clause is true, the result will be true. With this information, it is recommended to use the regular conditional operators most times, since checking clause2 can sometimes be a waste of resources.
Was This Post Helpful? 0
  • +
  • -

#3 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: bitwise and

Posted 29 March 2013 - 05:09 AM

If you google bitmasks you will find tutorials that will help you understand. Basically, if ITALIC is binary 01 and BOLD is binary 10 then BOLD and ITALIC is binary 11.

You can extract the Italic part by doing a bitwise AND:

11 & ITALIC is the same as 11 & 01 which equals 01

This will either be equal to ITALIC or zero. You can check via:

if (result == ITALIC)

or putting it all together:

if ((fontStyle & Font.ITALIC) == Font.ITALIC)

Suffice it to say, this is a horrible way to program in an object orientated language. I can only assume that something at a lower level needs bitmasks. It's a shame they exposed it to the programmer in the API. You should use the Font methods isBold() and isItalic() instead.
Was This Post Helpful? 3
  • +
  • -

#4 CY5   User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 413
  • Joined: 28-September 12

Re: bitwise and

Posted 29 March 2013 - 05:12 AM

View PostFlukeshot, on 29 March 2013 - 05:05 AM, said:

if(clause1 &  clause2)//checks clause 2 regardless of the result of clause1.
if(clause1 && clause2)//checks clause 2 only if the result of clause1 is true.

if(clause1 |  clause2)//checks clause 2 regardless of the result of clause1.
if(clause1 || clause2)//checks clause 2 only if the result of clause1 is false.

it means that
   if(fontstyle & Font.ITALIC)==Font.Italic


fontstyle may be bold or plain,if condition is true always.
Please tell me if i misunderstood you
Was This Post Helpful? 0
  • +
  • -

#5 Flukeshot   User is offline

  • A little too OCD
  • member icon

Reputation: 418
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: bitwise and

Posted 29 March 2013 - 05:49 AM

View PostCY5, on 29 March 2013 - 12:12 PM, said:

it means that
   if(fontstyle & Font.ITALIC)==Font.Italic


fontstyle may be bold or plain,if condition is true always.
Please tell me if i misunderstood you


The result remains the same for both types, all that's different is that && and || take a "shortcut" if the first result makes it impossible for the result to change.

true || false always returns true, it can't return false because there is a true - EITHER can be true, so if(true || why try to make this false when you can't?)

false && true always returns false, it can't return true because there is a false - BOTH must be true, so if(false && why try to make this true when you can't?)

This is the difference from the perspective of a beginner, there are advanced technical differences - see cfoley's post.

Edit: typo

This post has been edited by Flukeshot: 29 March 2013 - 05:50 AM

Was This Post Helpful? 1
  • +
  • -

#6 CY5   User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 413
  • Joined: 28-September 12

Re: bitwise and

Posted 29 March 2013 - 06:19 AM

Thanks to cfoley and flukeshot

View Postcfoley, on 29 March 2013 - 05:09 AM, said:

You should use the Font methods isBold() and isItalic() instead.

please frame this in if condition
Was This Post Helpful? 0
  • +
  • -

#7 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: bitwise and

Posted 29 March 2013 - 06:25 AM

Hang on, those "advanced technical differences" are actually really important. If you look at the expression CY5 was asking about, the bitwise and is not used as a conditional operator at all. Saying it's nearly equivalent to the shortcut operator is incorrect and misleading.

The & CAN be used as a conditional but that's not what is happening here.

Let me rewrite that piece of code:

// First let's be clear that the fontStyle is an int that has
//flags for bold, italics and maybe other things.
int combinedItalicAndBold = fontStyle;

// Let's also be clear that the value in Font.ITALIC is also an int.
// The bits to do with italicness are set to 1, the rest are set to zero.
int italicMask = Font.ITALIC;


// Here we use a bitmask to extract the information regarding italics.
// Note that the value is an int, NOT a boolean. All that has happened
// is that all the binary digits that are not to do with the italics
// are set to zero.
int maybeItalic = (combinedItalicAndBold & italicMask);

// Now we convert that value to a boolean using ==.
// Because of the way the bitmask is set up, we can be confident that
// maybeItalic will either have the same value as italicValue (in which
// case isItalic is true) or maybeItalic is zero (in which case
// isItalic is false).
boolean isItalic = (maybeItalic == italicValue);

// Now that we have an boolean, we can use it in an if
if(isItalic) {
  // Do something because it is italic
} else {
  // Do something different because it's not italic.
}



This code is equivalent to the one-liner in the opening post in the thread.

View PostCY5, on 29 March 2013 - 01:19 PM, said:

Thanks to cfoley and flukeshot

View Postcfoley, on 29 March 2013 - 05:09 AM, said:

You should use the Font methods isBold() and isItalic() instead.

please frame this in if condition


Something like:

Font f = g.getFont();
if (f.isItalic()) {
  // Do something
}

Was This Post Helpful? 2
  • +
  • -

#8 CY5   User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 413
  • Joined: 28-September 12

Re: bitwise and

Posted 29 March 2013 - 06:29 AM

thanks cfoley
Was This Post Helpful? 0
  • +
  • -

#9 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: bitwise and

Posted 29 March 2013 - 08:41 AM

Quote

Suffice it to say, this is a horrible way to program in an object orientated language. I can only assume that something at a lower level needs bitmasks. It's a shame they exposed it to the programmer in the API. You should use the Font methods isBold() and isItalic() instead.
That's true in spirit but bear in mind that an int bitmask can OR in a lot of attributes. The OO approach of today would probably be java.util.EnumSet.contains. And CYS, looking at that class and what it does would be a good way in to explaining the functionality of the code in question
Was This Post Helpful? 0
  • +
  • -

#10 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: bitwise and

Posted 29 March 2013 - 08:53 AM

Quote

That's true in spirit but bear in mind that an int bitmask can OR in a lot of attributes.


Yes, but a nicely designed class or EnumSet can deal with a lot more... or less, as required.

I can almost see the point for programming embedded devices, but I can't imagine that was a consideration for the Font class.
Was This Post Helpful? 0
  • +
  • -

#11 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: bitwise and

Posted 29 March 2013 - 09:32 AM

Quote

Yes, but a nicely designed class or EnumSet can deal with a lot more... or less, as required.
Yes but back in the day - the EnumSet wasn't born ;)
Was This Post Helpful? 0
  • +
  • -

#12 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: bitwise and

Posted 29 March 2013 - 11:31 AM

But there were fields and methods. I don't see the obsession with cramming so much into a single int. Using a Font instance entails having a font and document in memory. How does saving a few bits by cramming elements of style together help?
Was This Post Helpful? 0
  • +
  • -

#13 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: bitwise and

Posted 30 March 2013 - 07:35 AM

Quote

I don't see the obsession with cramming so much into a single int.
Well i don't think you're considering the full functionality and importance of a bit mask. Even with EnumSet and the like it's not easy to reproduce all functionality. For instance, how are you going to implement, say, the mutual exclusivity of Font.BOLD and Font.PLAIN? Easy with a bitmask ...
Was This Post Helpful? 0
  • +
  • -

#14 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: bitwise and

Posted 30 March 2013 - 07:58 AM

True, but in Java you have always had this which might be verbose but at least presents a sane public API:

public class FontStyle {
	private boolean isBold, isItalic;

	public FontStyle() {
		this(false, false);
	}

	public FontStyle(boolean bold, boolean italic) {
		isBold = bold;
		isItalic = italic;
	}

	public void setAsPlain() {
		setBold(false);
		setItalic(false);
	}

	public void setBold(boolean bold) {
		isBold = bold;
	}

	public void setItalic(boolean italic) {
		isItalic = italic;
	}

	public boolean isPlain() {
		return (!isBold()) && (!isItalic());
	}

	public boolean isBold() {
		return isBold;
	}

	public boolean isItalic() {
		return isItalic;
	}

}


These days you could get a lot of mileage out of this, although it breaks down if you have even a few more options:

public enum FontStyle {
	PLAIN, BOLD, ITALIC, BOLD_ITALIC;
}

Was This Post Helpful? 0
  • +
  • -

#15 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: bitwise and

Posted 30 March 2013 - 12:58 PM

Well, that's the point:

EnumSet<FontStyle> styles = EnumSet.of(FontStyle.PLAIN, FontStyle.BOLD);


(which is possible) would right away give you broken code, as i mentioned in my last
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2