# CS 24000 Study Guide - Midterm Guide: Bitwise Operation

13 views2 pages
4 Feb 2017
School
Course
Professor

CS240&Lecture&5&Notes&review&2&
Cast%conversion%
Done&by&the&programmer&&
o (type)&expression&&
o (int)&2.5&results&into&2&&
&
Typedef%
typedef&provides&a&synonym&of&an&existing&type&&
o typedef int Boolean;
o Boolean b;
#define FALSE 0
#define TRUE 1
b = 1;
b = FALSE;
b = TRUE;
&
Common%errors%
“a”&and&‘a’&are&different&&
o “a”&is&a&string&constant&type&(char&*)&
o &‘a’&is&a&char&constant&type&(int)&&
if (c = getchar() != EOF)&is&not&the&same&as&if ((c =
getchar()) != EOF)&
&
Bit%operations:%left%and%right%shift%<<>>%
x&>>&i&&
o Shifts&bits&of&a&number&x&to&the&right&i&positions&&&
x&<<&i&&
o Shifts&bits&of&a&number&x&to&the&left&i&positions&&&
Example:&&
int i, j; !
i = 5; // In binary i is 00000101!
j= (5 << 3); // In binary j is 00101000
printf(“i=%d j=%d\n”); // Output: i=5 j=40 !
x<<i is equivalent to x*2^i !
x>>i is equivalent to x/2^i !
&
Bitwise%operations:%or%|%
The&“|”&operator&executes&“OR”&bit&operation.&&
unsigned x = 0x05; // 00000101
unsigned y = (x | 0x2);
// 00000101 | 00000010=00000111
printf(“x=0x%x 0x%x\n”, x,y); // x=0x5 y=0x7
Example:
/usr/include/unistd.h:
#define O_WRITE 0x02
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.