Docsity

Bit-wise Operators and Shifting in C, C++, Java: A Comprehensive Guide, Study notes of Computer Architecture and Organization

Toggle sidebar
bg1
bottom bits disappear...
if unsigned, right shift in zeros
01
unsigned char a = 0x55; 010101
unsigned char b = 0x0f; 01010101
remember hexadecimal?
Bit-wise Logical Operators (in C, C++, Java, many other languages)
unsigned char c = a | b;
Low-level Programming in High-level Languages (CS232 Lecture 1)
01010101
01010101
11 110101
OR
unsigned char d = a & b;
01010101
01010101
0101
AND
0 00 0
unsigned char e = a ^ b;
01010101
01010101
110101
XOR
0 0
Shifting (in C, C++, Java, many other languages)
unsigned char g = f >> 2;
0 00 00
top bits disappear...
always left shift in zeros
unsigned char f = b << 5;
01010101
00 000
11
11 1
00 00011 1
1
bottom bits disappear...
if signed, sign extend MSB
unsigned char i = h >> 2;
00011
00 00011 1
1
signed char h = f;
11
(bit-wise OR) (bit-wise AND)
(bit-wise XOR)
(left shift)
(right shift logical)
(right shift arithmetic)
unsigned char n = ~a;
01010101
11
NOT
0 0
(bit-wise NOT)
11 0 0
Note: x >> 1 not the same as x/2 for negative numbers; compare (-3)>>1 with (-3)/2

Partial preview of the text

Download Bit-wise Operators and Shifting in C, C++, Java: A Comprehensive Guide and more Study notes Computer Architecture and Organization in PDF only on Docsity!

bottom bits disappear...

if unsigned, right shift in zeros

unsigned char a = 0x55;

unsigned char b = 0x0f;

remember hexadecimal?

Bit-wise Logical Operators (in C, C++, Java, many other languages)

unsigned char c = a | b;

Low-level Programming in High-level Languages (CS232 Lecture 1)

OR

unsigned char d = a & b;

AND

unsigned char e = a ^ b;

XOR

Shifting (in C, C++, Java, many other languages)

unsigned char g = f >> 2;

top bits disappear...

unsigned char f = b << 5; always left shift in zeros

bottom bits disappear...

if signed, sign extend MSB

unsigned char i = h >> 2;

signed char h = f;

(bit-wise OR) (bit-wise AND)

(bit-wise XOR)

(left shift)

(right shift logical)

(right shift arithmetic)

unsigned char n = ~a;

NOT

(bit-wise NOT)

Note: x >> 1 not the same as x/2 for negative numbers; compare (-3)>>1 with (-3)/