Bit Manipulations CS212.

Slides:



Advertisements
Similar presentations
7-5 Microoperation An elementary operations performed on data stored in registers or in memory. Transfer Arithmetic Logic: perform bit manipulation on.
Advertisements

Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS
Binary Logic (review) Basic logical operators: (Chapter 7 expanded)
C expressions (Reek, Ch. 5) 1CS 3090: Safety Critical Programming in C.
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Bitwise Operations CSE 2451 Rong Shi. Working with bits – int values Decimal (not a power of two – used for human readability) – No preceding label –
MICRO OPERATIONS Department of Computer Engineering, M.S.P.V.L. Polytechnic College, Pavoorchatram.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to use the bitwise logical operators in programs ❏ To be able to use.
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
Bits and Bytes. BITWISE OPERATORS Recall boolean logical operators in Java… boolean logical operators: &, |, ^ not: ! Show truth tables.
1 CS103 Guest Lecture Number Systems & Conversion Bitwise Logic Operations.
Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.
Number Systems Binary to Decimal Octal to Decimal Hexadecimal to Decimal Binary to Octal Binary to Hexadecimal Two’s Complement.
©Brooks/Cole, 2003 Chapter 4 Operations on Bits. ©Brooks/Cole, 2003 Apply arithmetic operations on bits when the integer is represented in two’s complement.
Computer Organization and Assembly Language Bitwise Operators.
REGISTER TRANSFER LANGUAGE MICROOPERATIONS. TODAY OUTLINES Logic Microoperations Shift Microoperations.
Bitwise Operators in C. Bitwise operators are used to manipulate one or more bits from integral operands like char, int, short, long.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
Bit Operations Horton pp 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.
Chapter 1  Number Systems Decimal System Binary System Octal System Hexadecimal System  Binary weighted cods Signed number binary order  1’s and 2’s.
1 Arithmetic and Logic Operations Patt and Patel Ch. 2 & 3.
Arithmetic Operations
20. LOW-LEVEL PROGRAMMING. Bitwise Shift Operators The bitwise shift operators are: > right shift The expression i
CS 151: Digital Design Chapter 4: Arithmetic Functions and Circuits
Bit Manipulation in 'C' 'C' bit operators
Microprocessor & Assembly Language
Chapter 8 Bit Operations By C. Shing ITEC Dept Radford University.
Computer and Information Sciences College / Computer Science Department CS 206 D Computer Organization and Assembly Language.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
Data Representation. Representation of data in a computer Two conditions: 1. Presence of a voltage – “1” 2. Absence of a voltage – “0”
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
From bits to bytes to ints
Lesson Objectives Aims
Integer Real Numbers Character Boolean Memory Address CPU Data Types
CSCI206 - Computer Organization & Programming
Boolean Algebra, Bitwise Operations
ECE 103 Engineering Programming Chapter 3 Numbers
Session #5 File I/O Bit Masks, Fields & Bit Manipulations
Bit Operations Horton pp
Chapter 3 Bit Operations
Number Systems and Bitwise Operation
University of Gujrat Department of Computer Science
What to bring: iCard, pens/pencils (They provide the scratch paper)
Logic Bitwise Instructions
Introduction to Programming and the C Language
Chapter 14 Bitwise Operators Objectives
REGISTER TRANSFER LANGUAGE
Computer Architecture & Operations I
The University of Adelaide, School of Computer Science
Bitwise Operators CS163 Fall 2018.
Embedded Programming in C
CS-401 Computer Architecture & Assembly Language Programming
Shift & Rotate Instructions)
Embedded Programming in C
Logical Operations Boy who sow wild oats better hope for crop failure.
Bit Fields & Bitwise Operations
Shift & Rotate Instructions)
Homework Homework Continue Reading K&R Chapter 2 Questions?
Comp Org & Assembly Lang
Binary Addition (1 of 2) Two 1-bit values A B A + B 1
Module 10 Operations on Bits
Microprocessor and Assembly Language
Bitwise Operators.
Immediate data Immediate operands : ADD r3, r3, #1 valid ADD r3, #1,#2 invalid ADD #3, r1,r2 invalid ADD r3, r2, #&FF ( to represent hexadecimal immediate.
GCSE COMPUTER SCIENCE Topic 3 - Data 3.3 Logical and Arithmetic Shifts.
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
Logical Operations Boy who sow wild oats better hope for crop failure.
Bit Operations Horton pp
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

Bit Manipulations CS212

Bit Level Manipulators And (&) bitwise based on type Or ( | ) Xor ( ^ ) Compelment ( ~ ) Shift right ( >> ) no carry, in or out; based on type shift left ( << )

Bases other than 10 Binary ; base 2 ; ( 0,1) char myint = 0b00001111; // 1510 Octal ; base 8 ; (0,1,2,3,4,5,6,7) char myint2 = 0o017; // 1510 Hexadecimal ; base 16 ; (0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f) char myint3 = 0x0f ; // 1510

Shifting Logical shifts – shift in a 0 as pad character Arithmetic shifts – preserve the sign bit for right shift, act like a logical shift for left shift Rotate left – lsb becomes the msb Rotate right – msb becomes lsb In C and C++ : shifting an unsigned int acts as a logical shift shifting a signed int acts as an arithmetic shift rotates are not part of the language and must be done via a user provided function

Shifting unsigned char a; a = 0b00100010 << 2 ; // produces 0b1000 1000 unsigned char b; b = 0xaa >> 1; // b = 0x55 //turn on bit 5 mask = ( 1 << 5 );

And & 1 Used for masking or extracting bits in a variable or 1 Used for masking or extracting bits in a variable or testing the status of a bit or a field in a variable Also for turning bits off // check to see if bit 3 in int1 is set to 1 char int1 = 0xaa; char mask3 = 0b00001000; if ( int1 & mask3) printf(“bit 3 on”); else printf(“bit3 off); // turn bits 0 and 1 off in int4 char int4 = 0b10111011; char mask01 =0b11111100; int4 &=mask01 ; //result is int4 = 0b10111000; //clear a bit unsigned char b &= ~(1 << n);

Or | 1 Usually used for turning bits on // turn on bit 3 1 Usually used for turning bits on // turn on bit 3 char register = 0xaa; char bit3 = 0b00000100 ; register |= bit3; //register is now 0b10100100; // turn on bit n in a unsigned char a |= (1 << n);

Xor ^ 1 Called a half add or an add with no carry; can be used to flip bits // flip bit n in word unsigned char word ^= (1 << n); //set word to all zeros , xor it with itself word ^= word; //swap the values in a and b (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))

Complement ~ 1 Used to flip all of the bits in a variable 1 Used to flip all of the bits in a variable unsigned char int1 = 0xaa; int1 = ~int1 // produces int1 = 0x55;