Bitwise operations
Download
1 / 16

Bitwise Operations - PowerPoint PPT Presentation


  • 74 Views
  • Uploaded on

Bitwise Operations. CSE 2451 Matt Boggus. Working with bits – int values. Decimal (not a power of two – used for human readability) No preceding label Valid digits: 0-9 int x = 22; Hexidecimal (2 4 or 4 bits) Starts with 0x Valid digits: 0-9 and A-F int y = 0xFF12;

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Bitwise Operations' - december


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Bitwise operations

Bitwise Operations

CSE 2451

Matt Boggus


Working with bits int values
Working with bits – int values

  • Decimal (not a power of two – used for human readability)

    • No preceding label

    • Valid digits: 0-9

    • int x = 22;

  • Hexidecimal(24or 4 bits)

    • Starts with 0x

    • Valid digits: 0-9 and A-F

    • int y = 0xFF12;

  • Octal (23or 3 bits)

    • Starts with 0

    • Valid digits: 0-7

    • int w = 067;

  • Binary (21or 1 bit)

    • Starts with 0b

    • Valid digits: 0,1

    • int z = 0b01001;


Bitwise operations1
Bitwise Operations

  • Operate on the bits of a data word

  • Corresponding bits of both operands are combined by the usual logic operations

  • Apply to integer types, not floats


Why use bitwise operators
Why use bitwise operators?

  • Cryptography

  • Compression algorithms

  • Computer graphics

  • Embedded devices and data storage efficiency

  • Hash functions

  • Network protocols

  • Not necessarily faster than arithmetic operations

    • Some nifty tricks though


Bitwise operators
Bitwise operators

& AND Result is 1 if both operand bits are 1

| OR Result is 1 if either operand bit is 1

^ Exclusive OR Result is 1 if operand bits are different

~ One’s Complement Each bit is reversed

<< Shift left Move every bit left (multiply by 2)

>> Shift right Move every bit right (divide by 2)


Examples assuming an int is only 8 bits
Examples (assuming an int is only 8-bits)

intc, a, b;

a = 0b11110000;

b = 0b10101010;

c = a & b; // 1010 0000

c = a | b; // 1111 1010

c = a ^ b; // 0101 1010

c = ~a; // 0000 1111

c = a << 2; // 1100 0000

c = a >> 5; // 0000 0111

What are a and b’s decimal values?

Depends on if the leftmost or rightmost is the most significant bit. On stdlinux

a is 240

b is 170


And and or usage bit masking
AND and OR usage: bit masking


Xor usage swapping
XOR usage: swapping

temp = x;

x = y;

y = temp;

x = x ^ y;

y = x ^ y;

x = x ^ y;

Additional XOR uses and a proof for the validity of swapping


Bit shifts and overflows
Bit shifts and overflows

  • Shift k bits : multiply or divide by 2k

    • New bits “shifted in” are zeroes

  • Some bit shifts are poorly defined, their results are implementation dependent

    • a << -5

    • a << 493


Looping using bitwise operations
Looping using bitwise operations

int copy = n;

while (copy != 0)

{

// …do stuff with copy…

// how would you “access” the least significant bit?

// move every bit right

copy = copy >> 1;

}


Code example
Code example

Output is:

a is f0f0

b is 5555

a >> 4 is f0f

b >> 4 is 555

binary = 42

#include <stdio.h>

void main() {

unsigned inta,b,c,d,e;

a = 0xF0F0;

b = 0x5555;

c = a >> 4;

d = b >> 4;

e = 0b01000010;

// %x is unsigned int in hex

printf("a is %x\n",a);

printf("b is %x\n",b);

printf("a >> 4 is %x\n",c);

printf("b >> 4 is %x\n",d);

printf("binary = %x\n",e);

}

Note: printf does not directly support printing values in binary or octal form, only decimal and hex


Traditional bit definition
Traditional Bit Definition

#define EMPTY 01

#define JAM 02

#define LOW_INK 16

#define CLEAN 64

char status;

Example statements:

if (status == (EMPTY | JAM)) ...;

if (status == EMPTY || status == JAM) ...;

while (! status & LOW_INK) ...;

int flags |= CLEAN /* turns on CLEAN bit */

int flags &= ~JAM /* turns off JAM bit */


Traditional bit definitions
Traditional Bit Definitions

  • Used very widely in C

    • Including a lot of existing code

  • No checking

    • You are on your own to be sure the right bits are set

  • Machine dependent

    • Need to know bit order in bytes (8 bits), byte order in words (2, 4, … bytes)

  • Working with individual bits

    • Use AND and shift to extract

    • Use shift and OR to insert



Conditional operator if then else
Conditional Operator – If/Then/Else

  • Ternary operator ? :

  • Syntax bool ? exp1 : exp2

  • Evaluation

    • If bool is true (non-zero), the result is exp1

    • If boolis false (zero), the result is exp2

  • Example:

    int a = 10;

    int b = 20;

    x = a > b ? a : b; // x = b since a is less than b

  • Style

    • Rarely more readable than using if/else

    • Useful in macros – ex: #define MAX(a, b) (((a)>(b)) ? (a) : (b))


Comma operator when used in arithmetic expressions
Comma operator (when used in arithmetic expressions)

  • Syntax exp1, exp2, exp3, …, expn

  • Evaluate statements from left to right, result of the entire expression is expn (rightmost exp)

  • Example:

    // x is set to 10, y is set to 5, and value is set to 15

    value= (x = 10, y = 5, x + y);

  • Comma operator has lowest precedence – use parenthesis

  • Style

    • Rarely more readable than using multiple single expressions

    • Can be used to cause “side effects”

      • Side effect – a statement that modifies a variable or program state in addition to returning a value

      • Ex: while (c=getchar(), c!= ‘10’)

      • Usually result in code that is less readable