## Sunday, November 18, 2012

### C/C++ Logical and Bitwise Operators

Introduction

A simple note of C/C++ Logical and Bitwise Operators

Code

#include <iostream>
using namespace std;

/** some practice of logical operators
* and bitwise operators
*/
int main () {

cout << endl;
cout << "\tLogical operators" << endl;
// && (Logical AND)
// All true -> true
// Any false -> false
cout << "\ttrue && true && true\t -> \t" << (true && true && true? "true" : "false") << endl;
cout << "\ttrue && true && false\t -> \t" << (true && true && false? "true" : "false") << endl;
// || (Logical OR)
// Any true -> true
// All false -> false
cout << "\tfalse || false || true\t -> \t" << (false || false || true? "true" : "false") << endl;
cout << "\tfalse || false || false\t -> \t" << (false || false || false? "true" : "false") << endl;
// ! (Logical NOT)
// inverse the value
cout << "\t!true\t\t\t -> \t" << (!true? "true" : "false") << endl;
cout << "\t!false\t\t\t -> \t" << (!false? "true" : "false") << endl << endl;

cout << "\tBitwise operators" << endl;
// & (Bitwise AND)
// 0 & any -> 0
// 1 & 1 -> 1
// 3 & 5 -> 0011 & 0101 = 0001 -> 1
cout << "\t3 & 5\t\t\t -> \t" << (3 & 5) << endl;

// | (Bitwise Inclusive OR)
// 1 | any -> 1
// 0 | 0 -> 0
// 3 | 5 -> 0011 | 0101 = 0111 -> 7
cout << "\t3 | 5\t\t\t -> \t" << (3 | 5) << endl;

// ^ (Bitwise Exclusive OR)
// 0 ^ 0 -> 0
// 1 ^ 1 -> 0
// 1 ^ 0 -> 1
// 0 ^ 1 -> 1
// 3 ^ 5 -> 0011 ^ 0101 = 0110 -> 6
cout << "\t3 ^ 5\t\t\t -> \t" << (3 ^ 5) << endl;

// << (Shift Left)
// 0001 << 1 -> 0010
// 0001 << 2 -> 0100
// shift all bits to left side and add 0 to right side,
// the left side bits will be dropped,
// e.g., 100...001 << 1 -> 00...010
//
// can use it as *2 operation
// 3 << 1 -> 0011 << 1 = 0110 -> 6 (3*2)
// 3 << 2 -> 0011 << 2 = 1100 -> 12 (3*2*2)
cout << "\t3 << 1\t\t\t -> \t" << (3 << 1) << endl;
cout << "\t3 << 2\t\t\t -> \t" << (3 << 2) << endl;

// << (Shift Right)
// 0100 >> 1 -> 0010
// 0100 >> 2 -> 0001
// shift all bits to right side and
// add 0 or 1 (depends on system) to left side,
// the right side bits will be dropped,
// e.g., ...00011 >> 1 -> ...0001
//
// can use it as /2 operation
// 12 >> 1 -> 1100 >> 1 = 0110 -> 6 (12/2)
// 12 >> 2 -> 1100 >> 2 = 0011 -> 3 (12/2/2)
cout << "\t12 >> 1\t\t\t -> \t" << (12 >> 1) << endl;
cout << "\t12 >> 2\t\t\t -> \t" << (12 >> 2) << endl;

// ~ (Unary complement (bit inversion))
// 0 -> 1
// 1 -> 0
// ~ (char) -128 -> ~10000000 = 01111111 -> 127
cout << "\t ~(char)-128\t\t -> \t" << ~(char)-128 << endl;
cout << endl;

system("PAUSE");
return 0;
}

Result

Reference

http://www.cplusplus.com/doc/tutorial/operators/
http://en.wikipedia.org/wiki/Bitwise_operation

File at github
https://github.com/benbai123/C_Cplusplus_Practice/blob/master/CPP/CPP_Basic/logical_and_bitwise_operator.cpp

## Wednesday, November 14, 2012

### Use Javascript to Print Content of a Div

Simple note

Copy all content to a new window (with styles if any) then call window.print()

Code

<html>
<script type="text/javascript">
function printContent (divId) {
// open a new window
var mywindow = window.open('', '', 'height=400,width=600'),
mwindoc = mywindow.document;
// write content to new window
i;

// add styles to new window
for (i = 0; i < links.length; i++) {
+ ' />';
mwindoc.write(lcnt);
}
// add contents to print to new window
// the divId is the assigned div id
mwindoc.write(document.getElementById(divId).innerHTML);
mwindoc.write('</body></html>');

// call print function then close the new window
mywindow.document.close();
mywindow.focus();
mywindow.print();
mywindow.close();

return true;
}
</script>
<div id="content">
<div style="color: green; font-size: 36px;">
The content to print
</div>
</div>
<button onclick="printContent('content');">Print</button>
</html>

Result

Print window (not browser window) appeared after 'Print' button clicked

References

http://stackoverflow.com/questions/2255291/print-the-contents-of-a-div

http://stackoverflow.com/questions/2555697/window-print-not-working-in-ie

File at github
https://github.com/benbai123/HTML_CSS_Javascript_practice/blob/master/javascript/use_javascript_to_print_contents_of_a_div.html

## Saturday, November 10, 2012

### C++ Conditional Operator

Simple note

The non-zero value or non-null pointer will be considered as true, zero and null will be considered as false.

Code

#include <iostream>
using namespace std;

/** All non-zero value/non-null pointer will be
* considered as "true" in conditional operator
*/
class Base {
public:
int a;
Base ()
{
a = 1;
}
};
int main () {
bool bbb = true;
Base baseInst;
Base* baseNull = NULL;
cout << endl
<< "\t3 ? \t\t" << (3? "true" : "false") << endl // not zero, true
<< "\t-1 ? \t\t" << (-1? "true" : "false") << endl // not zero, true
<< "\t&baseInst ? \t" << (&baseInst? "true" : "false") << endl // pointer and not null, true
<< "\t\"test\" ? \t" << ("test"? "true" : "false") << endl
<< "\t0 ? \t\t" << (0? "true" : "false") << endl // zero, false
<< "\tbaseNull ? \t" << (baseNull? "true" : "false") << endl << endl; // null pointer, false

system("PAUSE");
return 0;
}

Result

Reference

http://www.cplusplus.com/forum/lounge/6307/

File at github
https://github.com/benbai123/C_Cplusplus_Practice/blob/master/CPP/CPP_Basic/conditional_operator.cpp

## Tuesday, November 6, 2012

### C++ literal constant, type and format

Simple note:

Code:

literal_constant_test.cpp

#include <iostream>
#include <iomanip>
using namespace std;

/** test literal constant,
* also try show their type and format them with cout
*/
int main() {
cout << "\tvalue - type: " << endl << endl << "\t"
<< 11 << " - " << typeid(11).name() << "(int)" << endl << "\t" // int
<< 011 << " - " << typeid(011).name() << "(int)" << endl << "\t" // int, octonary
<< 0x11 << " - " << typeid(0x11).name() << "(int)" << endl << "\t" // int, hex
<< 11U << " - " << typeid(11U).name() << "(unsigned int)" << endl << "\t" // unsigned int
<< 11L << " - " << typeid(11L).name() << "(long)" << endl << "\t" // long
<< 11UL << " - " << typeid(11UL).name() << "(unsigned long)" << endl << "\t" // unsigned long
<< setprecision(2) << fixed // change to fixed format, 11.0 -> 11.00
<< 11.0 << " - " << typeid(11.0).name() << "(double)" << endl << "\t" // double
<< noshowpoint << setprecision( 0 )  // change to no point, 11.0 -> 11
<< 1.1e1 << " - " << typeid(1.1e1).name() << "(double)" << endl << "\t" // double
<< scientific // change to scientific notation, 11.0 -> 1.10e+001
<< 11.0F << " - " << typeid(11.0F).name() << "(float)" << endl << endl; // float
system("PAUSE");
return 0;
}

Result:

References:

http://stackoverflow.com/questions/554063/how-do-i-print-a-double-value-with-full-precision-using-cout

http://stackoverflow.com/questions/81870/print-variable-type-in-c

http://www.cplusplus.com/reference/iostream/manipulators/fixed/

http://www.cplusplus.com/reference/iostream/manipulators/noshowpoint/

File at github:

https://github.com/benbai123/C_Cplusplus_Practice/blob/master/CPP/CPP_Basic/literal_constant_test.cpp

## Monday, November 5, 2012

### unsigned Keyword in C/CPP

Simple note:

Code:

#include <iostream>
using namespace std;

/** test unsigned keyword,
* use char to test it since its range is fixed ( 1 byte )
*/
int main() {
char ch = -1; // signed char, range -128 ~ 127
unsigned char uch = ch; // unsigned char, range 0 ~ 255
cout << "\n\tsigned: " << static_cast<int>(ch) << "\n"
<< "\tunsigned: " << static_cast<int>(uch) << "\n\n";
system("PAUSE");
return 0;
}

Result:

File at github:

https://github.com/benbai123/C_Cplusplus_Practice/blob/master/CPP/CPP_Basic/unsigned_test.cpp

## Sunday, November 4, 2012

### ZK ID Conflict and Component IdSpace

Introduction

From official site:
An ID space is a subset of components of a desktop. The uniqueness is guaranteed only in the scope of an ID space. Thus, developers could maintain the subset of components separately without the need to worry if there is any conflicts with other subsets.
...
Window is a typical component that is an ID space. ... Thus, you could use a window as the topmost component to group components. This way developers only need to maintain the uniqueness of each subset separately.

This article will present the ID conflict problem and how to prevent it by ID space

Pre-request

Regarding setting up the environment to run ZK, please refer to http://ben-bai.blogspot.tw/2012/06/zk-quick-start.html

Test Code and Result

id_conflict_in_zul_page.zul

There are two textbox with id "tbx" in this page, this will cause ID conflict

<zk>
<button label="test">
<attribute name="onClick">
tbx.setValue("button 'test' is clicked");
</attribute>
</button>
<textbox id="tbx" />
<button label="test2">
<attribute name="onClick">
tbx.setValue("button 'test2' is clicked");
</attribute>
</button>
<!-- the id "tbx" already used in previous textbox,
will throw an org.zkoss.zk.ui.UiException
with respect to "Not unique in ID space" -->
<textbox id="tbx" />
</zk>

use_idspace_to_prevent_id_conflict.zul

There are also two textbox with id "tbx" in this page, but they are under different window component so will not cause ID conflict.

<zk>
<!-- both buttons call tbx.setValue(...) when clicked,
they will set the value into the component with id "tbx"
under the same IdSpace -->
<window>
<button label="test">
<attribute name="onClick">
tbx.setValue("button 'test' is clicked");
</attribute>
</button>
<textbox id="tbx" />
</window>
<window>
<button label="test2">
<attribute name="onClick">
tbx.setValue("button 'test2' is clicked");
</attribute>
</button>
<!-- the id "tbx" already used in previous textbox,
but the previous textbox is in different IdSpace
(under different window), the page will be
rendered correctly -->
<textbox id="tbx" />
</window>
</zk>

The result of this page after buttons clicked:

Reference

ZK Developer's Reference/UI Composing/ID Space
http://books.zkoss.org/wiki/ZK_Developer's_Reference/UI_Composing/ID_Space