CS 24000 Study Guide - Midterm Guide: Bitwise Operation

13 views2 pages

For unlimited access to Study Guides, a Grade+ subscription is required.

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_READ 0x01
#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.

Already have an account? Log in

Get access

Grade+
$10 USD/m
Billed $120 USD annually
Homework Help
Class Notes
Textbook Notes
40 Verified Answers
Study Guides
1 Booster Class