0% found this document useful (0 votes)
16 views

6.0 Bit Operations

Uploaded by

salcanmehmet10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

6.0 Bit Operations

Uploaded by

salcanmehmet10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Bit Operations

CS204
Week 9
Kamer Kaya
Representation of Data
All data in a computer’s memory and files are represented as a sequence of bits

Bit : unit of storage, represents the level of an electrical charge. Can be either 0 or 1

Byte: another unit of storage that occupies 8 bits.

A bit sequence can represent many different things:


– We will see that a bit string (such as 10000000111000001011111110100010)
can mean several different things depending on the representation that is
agreed upon.

So, how should to represent integers, characters, real numbers, strings,


structures, in terms of bits?
– Representations must be efficent and convenient
– We will see some of them
Characters
In C/C++, characters are actually integers of length one byte, with special meaning
as characters
- the ASCII mapping

char c = 'a'; //stores the code corresponding to letter ‘a’, but


//prints the character a when printed

ASCII Standard
– American Standard Code for Information Interchange
– dates back 1960's
– 256 different codes (0 . . . 255) and corresponding characters
– The characters with codes 0 . . 31, and 127 are control characters
• At these times, standardizing communication related and telegraphic codes was important.
That is why most of the control characters are for this purpose and now obselete. Though,
some OSs implement some of the control chars.
– Extended ASCII (128 – 255): there are different conventions and interpretations
See http://www.asciitable.com/ and http://en.
ASCII wikipedia.org/wiki/ASCII for more info
Blue control These are the ASCII codes – of course you are not expected to memorize them,
characters are
just know that there are codes for special characters and that numbers and
the ones
important for letters of a case are consecutive (so one can do '1'+5 to get code of '6', or
Windows/DOS subtract 32 to go from lowercase to get corresponding uppercase)
| 0 NUL| 1 SOH| 2 STX| 3 ETX| 4 EOT| 5 ENQ| 6 ACK| 7 BEL|
| 8 BS | 9 HT | 10 LF | 11 VT | 12 FF | 13 CR | 14 SO | 15 SI |
| 16 DLE| 17 DC1| 18 DC2| 19 DC3| 20 DC4| 21 NAK| 22 SYN| 23 ETB|
| 24 CAN| 25 EM | 26 SUB| 27 ESC| 28 FS | 29 GS | 30 RS | 31 US |
Special
control | 32 SP | 33 ! | 34 " | 35 # | 36 $ | 37 % | 38 & | 39 ' |
characters. | 40 ( | 41 ) | 42 * | 43 + | 44 , | 45 - | 46 . | 47 / |
Most of | 48 0 | 49 1 | 50 2 | 51 3 | 52 4 | 53 5 | 54 6 | 55 7 |
them are | 56 8 | 57 9 | 58 : | 59 ; | 60 < | 61 = | 62 > | 63 ? |
now | 64 @ | 65 A | 66 B | 67 C | 68 D | 69 E | 70 F | 71 G |
obselete. | 72 H | 73 I | 74 J | 75 K | 76 L | 77 M | 78 N | 79 O |
Some OSs
| 80 P | 81 Q | 82 R | 83 S | 84 T | 85 U | 86 V | 87 W |
implement
some of | 88 X | 89 Y | 90 Z | 91 [ | 92 \ | 93 ] | 94 ^ | 95 _ |
them and | 96 ` | 97 a | 98 b | 99 c |100 d |101 e |102 f |103 g |
meanings |104 h |105 i |106 j |107 k |108 l |109 m |110 n |111 o |
may |112 p |113 q |114 r |115 s |116 t |117 u |118 v |119 w |
change |120 x |121 y |122 z |123 { |124 | |125 } |126 ~ |127 DEL|
from OS to
OS
Why we need to work with bits
Sometimes one bit is enough to store your data: say the gender of the
student (e.g. 0 for men, 1 for women). We don’t have a 1-bit variable
type in C++, so for gender, you will have to use an unsigned char or
char type variable.

But if you need say 8 such 1-bit variables, say to record if the student were
present in the times when attendance was taken, then you can actually
combine all into one char variable.
class student {

private:
unsigned char attendance;
//now I can fit 8 bits into this, we will see how
}

In C++, there is special container class, called bitset, that is designed to store bits.
But we will not see and use it. And you are not allowed to use it in this course.
Packing bits
“Packing” 8 1-bit variables into 1 char variable is easy.

Say you know that the student were present in the first 3 class and not in the last 5. The variable attendance can be set as:

unsigned char attendance = 7;

0 0 0 0 0 1 1 1

where the lest significant 3 bits represent the first 3 attendances (just a choice, it
could be the other way around as well).

But in addition to being able to set a variable’s value, we need to be able to handle
each bit separately, for which we need bit operators.
Hexadecimal literals and output
In C++ a hexadecimal literal (number) is represented by preceeding 0x before the
literal.

E.g. 0xb4a2 // equivalent to 11x163 + 4x162 + 10x16 + 2 = 46242 (decimal)

char ch = 0x7c; // ch contains 124 (7x16+ 12)

When you want to display a numeric value in hexadecimal, use hex in cout before the
value to be displayed.
–This affects all future integer outputs to be displayed in hexadecimal, so if you want to
switch back to decimal, use dec later.

short nums = -200;


cout << "hexadecimal: " << hex << nums << " decimal: " << dec << nums << endl;
Output: hexadecimal: ff38 decimal: -200

unsigned int numi = 0xfd04e0e4;


cout << "hexadecimal: " << hex << numi << " decimal: " << dec << numi << endl;
Output: hexadecimal: fd04e0e4 decimal: 4244955364
Bit Operators
& Bitwise and
| Bitwise or
^ Bitwise exclusive or
~ Complement (unary operator, takes only one operand)
<< shift left
>> shift right

Do not confuse && with &


|| with |
Bitwise Operations: AND
Take the AND of the two numbers, bit by bit. a b a &b
0 0 0
0 1 0
char x,y,z;
1 0 0
x = 0xd5;
1 1 1
y = 0x6c;
z = x&y;
1 1 0 1 0 1 0 1
x

y 0 1 1 0 1 1 0 0

z 0 1 0 0 0 1 0 0
Bitwise AND
char c1, c2, c3;

c1 = 0x45;
c2 = 0x71;
c3 = c1 & c2;

c1 : 0100 0101
c2 : 0111 0001
c3 : 0100 0001 (=0x41 = 4x16+1 = 65 10)
Bitwise OR
Take the OR of the two numbers, bit by bit. a b a |b
0 0 0
char c1, c2, c3; 0 1 1
1 0 1
c1 = 0x45; 1 1 1
c2 = 0x71;
c3 = c1 | c2;

c1 : 0100 0101
c2 : 0111 0001
c3 : 0111 0101 (=0x75 = 7x16+5 = 11710 )
Bitwise XOR
Take the Exclusive OR of the two numbers, bit by bit.

char c1, c2, c3; a b a^b


0 0 0
c1 = 0x45; 0 1 1
c2 = 0x71;
1 0 1
c3 = c1 ^ c2;
1 1 0

c1 : 0100 0101
c2 : 0111 0001
c3 : 0011 0100 (=0x34 = 3x16+4 = 5210 )
Bitwise Complement
Complement operation (~) converts bits with value 0 to 1 and bits with value
1 to 0.
a ~a
0 1
1 0

char b1 = 0xc1; // 1100 0001


char b4 = 0x08; // 0000 1000
b4 = ~b1; // 0011 1110
What do these two statements do?

char x = 0xA5;

if ( x & 0x08 )
//what does this mean?

x = x | 0x02;
//what happened to x?

These are two of the most important bit operations! We will see more later,
but basically you can access a particular bit and you can set a particular
bit with these two operations.
Logic Operators versus Bitwise Logic Ops.
The operators && , || and ! are not bitwise logic operators!

– The result of ! , && and || operations is an integral data type with the
value 0 (every bit is a zero) or 1 (LeastSignificant bit is 1, all the others are 0).
Shift Operators

Shift operators move bits left or right, discarding bits from one side and filling
the other side with 0s.
<< means shift left
>> means shift right
how many times
Do not confuse with stream operators it is shifted
Bit operations: left shift
Suppose we want to shift the bits of a number N, k bits to the left
– denoted N << k

• drop leftmost k bits


• append k zeros to the right

Ex:
1 1 0 1 0 1 0 1
y = x << 1; x

y 1 0 1 0 1 0 1 0

Another example:
char c2 = 0x9C; //10011100
c = c2 << 2 //01110000
Bit operations: right shift

As opposed to left shift, the right shift works differently for signed and
unsigned numbers. We will talk about this later.
Self-Quiz
What are the resulting values of x,y and z?

char x,y,z;

x = 0x33;
x = (x << 3) | 0x0F;
y = (x >> 1) & 0x0F;
z = x && y;
Example for setting, testing, and clearing
the bits of bytes
const char IO_ERROR = 0x01; //LSB (right-most bit) is 1, others are 0
const char CHANNEL_DOWN = 0x10;
char flags = 0;

//if ever CHANNEL_DOWN event happens, set its corresponding bit in the flags variable
flags = flags | CHANNEL_DOWN; // set the 5th bit

...
This is also called masking
//to check what errors may have happened...
if ((flags & IO_ERROR ) != 0) // check the 1st bit
cout << "I/O error flag is set";
else if ((flags & CHANNEL_DOWN) != 0) // check the 5th bit
cout << "Channel down error flag is set";

...
flags = flags & ~IO_ERROR; // clear the ERROR flag
Example 2: Attendance
unsigned char attendance = 0x00;
//let the LSB be for quiz 1, the next one is for quiz 2, . . .
//0 means unattended, 1 means attended
//there are total of 8 quizzes; initally all unattended
unsigned char mask=0x01;
//Draft code showing how attendance array may be filled
//if attended to quiz 1
attendance = attendance | mask;
//if attended quiz 3
mask = mask << 2; // mask becomes 0000 0100
attendance = attendance | mask;
...
//to display attendance information
mask=0x01;
for (int i=1; i <= 8; i++)
{
if (attendance & mask)
cout << "Student attended quiz number " << i << endl;
else
cout << "Student missed quiz number " << i << endl;
mask = mask << 1;
}
21
Self Quiz 2
• What does the statements

if((x & (x – 1) == 0) && (x != 0)) {


//what does it mean when this is correct
}
and
x ^= y;
y ^= x;
x ^= y;
do?

22

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy