Bitwise Operations and Bitfields

Slides:



Advertisements
Similar presentations
Lecture 5: MIPS Instruction Set
Advertisements

CS/COE0447 Computer Organization & Assembly Language
INSTRUCTION SET ARCHITECTURES
The Binary Numbering Systems
Binary Logic (review) Basic logical operators: (Chapter 7 expanded)
Floating Point Numbers
Computer Structure - Computer Arithmetic Goal: Representing Numbers in Binary  Base 10 (decimal) - Numbers are represented using 10 numerals: 0, 1, 2,
Snick  snack A Working Computer Slides based on work by Bob Woodham and others.
CS 61C L02 Number Representation (1)Harvey / Wawrzynek Fall 2003 © UCB 8/27/2003  Brian Harvey ( John Wawrzynek  (Warznek) (
1 Representing Numbers Using Bases Numbers in base 10 are called decimal numbers, they are composed of 10 numerals ( ספרות ) = 9* * *10.
Computer Architecture CPSC 321 E. J. Kim. Overview Logical Instructions Shifts.
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
24/06/2015CSE1303 Part B lecture notes 1 Words, bits and pieces Lecture B05 Lecture notes section B05.
Logical & shift ops (1) Fall 2007 Lecture 05: Logical Operations.
Counters and Registers
Binary Arithmetic Math For Computers.
Karnaugh Maps By: Shakil Nobes.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Binary Arithmetic In today’s lesson we will look at: a reminder of how binary works adding binary numbers overflow complements negative numbers and subtraction.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
CSE378 Instr. encoding.1 Instruction encoding The ISA defines –The format of an instruction (syntax) –The meaning of the instruction (semantics) Format.
Moving Around in Scratch The Basics… -You do want to have Scratch open as you will be creating a program. -Follow the instructions and if you have questions.
CSCI 136 Lab 1: 135 Review.
Computer Organization and Assembly Language Bitwise Operators.
MIPS Logic & Shift. Bitwise Logic Bitwise operations : logical operations applied to each bit Bitwise OR:
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 9 Binary Representation and Logical Operations.
CSE 351 Number Representation. Number Bases Any numerical value can be represented as a linear combination of powers of n, where n is an integer greater.
CMSC 104, Lecture 061 Stored Programs A look at how programs are executed.
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
Programs, Instructions, and Registers
Week 2 - Wednesday CS 121.
CS/COE 0447 (term 2181) Jarrett Billingsley
Chapter 4 Operations on Bits.
Pseudo-ops and Bitwise Operations
Hexadecimal, Signed Numbers, and Arbitrariness
Lesson Objectives Aims
COMPUTER ARCHITECTURE & OPERATIONS I
Manipulating Bit Fields in C
CS/COE 0447 (term 2181) Jarrett Billingsley
Bitfields and Logic Basics
Binary Addition and Subtraction
CS/COE 0447 (term 2181) Jarrett Billingsley
The Interconnect, Control, and Instruction Decoding
Binary, Hex, and Arbitrariness
Binary, Hex, and Arbitrariness
Topic 3: Data Binary Arithmetic.
Addition, Signed Numbers, and Overflow
Lecture 4: MIPS Instruction Set
CS 240 – Lecture 9 Bit Shift Operations, Assignment Expressions, Modulo Operator, Converting Numeric Types to Strings.
Computer Architecture & Operations I
The University of Adelaide, School of Computer Science
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
Digital Logic.
Topic 3: Data Hexadecimal.
More Functions and the Stack
ECEG-3202 Computer Architecture and Organization
CS/COE 0447 Jarrett Billingsley
Multiplication and Division
The Interconnect, Control, and Instruction Decoding
Unit 10 Arithmetic-logic Units
Digital Logic.
FSMs, Multiplication, and Division
Arithmetic and Decisions
CS/COE 0447 Jarrett Billingsley
ECEG-3202 Computer Architecture and Organization
Number Representation & Operators
ECE 352 Digital System Fundamentals
Basic circuit analysis and design
Binary Numbers Material on Data Representation can be found in Chapter 2 of Computer Architecture (Nicholas Carter) CSIT 301 (Blum)
ECE 352 Digital System Fundamentals
Presentation transcript:

Bitwise Operations and Bitfields CS/COE 0447 Jarrett Billingsley

Class announcements new unit! sorta how's the project going CS447

Bitwise Operations CS447

What are "bitwise" operations? the "numbers" we use on computers aren't really numbers right? it's often useful to treat them instead as a pattern of bits bitwise operations treat a value as a pattern of bits 1 - encoding "booleans" as bits of a number is called a "bitmap" or "bitflags" and is a common way of saving space - much low-level hardware uses bitflags for controlling things, reading inputs etc. CS447

The simplest operation: NOT (logical negation) if the light is off, turn it on if the light is on, turn it off we can summarize this in a truth table we write NOT as ~A, or ¬A, or A A Q 1 CS447

Applying NOT to a whole bunch of bits if we use the not instruction (or ~ in C/Java), this is what happens: ~ 1 = 1 we did 8 independent NOT operations that's it it's super simple only 8 bits shown cause 32 bits on a slide is too much - ~ is NOT the same thing as ! - ! is "logical NOT" - it turns true to false and false to true CS447

Let's add some switches there are two switches in a row connecting the light to the battery how do we make it light up? CS447

A B Q 1 AND (Logical product) 1 & 1 = 1 A&B A∧B A⋅B AB AND is a binary (two-operand) operation it can be written a number of ways: A&B A∧B A⋅B AB if we use the and instruction (or & in C/Java): A B Q 1 1 & 1 = 1 we did 8 independent AND operations - it's logical product cause it looks like multiplication – anything times 0 is 0, and 1 x 1 is 1 CS447

"Switching" things up ;)))))))))))))))))))))) NOW how can we make it light up? CS447

A B Q 1 OR (Logical sum…?) 1 | 1 = 1 A|B A∨B A+B we might say "and/or" in English it can be written a number of ways: A|B A∨B A+B if we use the or instruction (or | in C/Java): A B Q 1 1 | 1 = 1 We did 8 independent OR operations. - it's logical sum cause it… kinda looks like addition, if you squint your eyes. - 1 + 1 = 1, okay? CS447

Bit shifting CS447

Bit shifting besides AND, OR, and NOT, we can move bits around, too. 1 1 0 0 1 1 1 1 if we shift these bits left by 1… 1 1 0 0 1 1 1 1 0 we stick a 0 at the bottom 1 1 0 0 1 1 1 1 0 0 again! 1 1 0 0 1 1 1 1 0 0 0 AGAIN! 1 1 0 0 1 1 1 1 0 0 0 0 AGAIN!!!! CS447

Left-shifting in C/Java and MIPS (animated) C and Java use the << operator for left shift B = A << 4; // B = A shifted left 4 bits MIPS has the sll (Shift Left Logical) instruction sll t2, t0, 4 # t2 = t0 << 4 if the bottom 4 bits of the result are now 0s… …what happened to the top 4 bits? 0011 0000 0000 1111 1100 1101 1100 1111 0000 Bit Bucket the bit bucket is not a real place it's a programmer joke ok - or… or maybe it could be………… CS447

<_< >_> we can shift right, too <_< >_> we can shift right, too 0 0 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 1 1 0 1 1 1 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 1 1 0 1 1 1 0 0 C/Java use >>, MIPS uses srl (Shift Right Logical) see what I mean about 32 bits on a slide CS447

Huh… that's weird Binary Decimal 00000101 5 00001010 10 00010100 20 let's start with a value like 5 and shift left and see what happens Binary Decimal 00000101 5 why is this happening well uh... what if I gave you 49018853 00001010 10 00010100 20 how do you multiply that by 10? 00101000 40 by 100? by 100000? 01010000 80 something very similar is happening here CS447

a << n = a × 2n shifting left by n is the same as multiplying by 2n you probably learned this as "moving the decimal point" and moving the decimal point right is like shifting the digits left with bit shifting, we're moving the binary point (yes, really) shifting is fast and easy on most CPUs way faster than multiplication in any case hey… if shifting left is the same as multiplying… CS447

Yep shifting right by n is like dividing by 2n Binary Decimal well, almost… 01010000 80 it's doing flooring division 00101000 40 normal int division works like this 00010100 20 5 / 2 = 2 00001010 10 00000101 5 00000010 2 CS447

Bitfields CS447

That's this color, in RGB565. The masters of meaning 23 32 19 15 11 10 what if we wanted to store multiple integers in one value? 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 red green blue decimal? 23 32 19 That's this color, in RGB565. CS447

whyyyyy??? it's smaller really, that's it but that's super important in a lot of cases smaller data… takes up less space in memory takes up less space in cache extremely important thing in modern CPUs that we talk about in 1541 is faster to move between memory and the CPU or between the CPU and anything else is faster to transfer across the internet and other networks CS447

I wanna turn the light on!! I have a sequence of 0s. I wanna turn one of them into a 1. what bitwise operation can I use to do that? ? 1 1 OR turns bits on. CS447

I wanna turn the light off!! I wanna turn one of the 1s into a 0. what bitwise operation can I use to do that? 1 1 1 ? 1 1 1 1 1 AND turns bits off. CS447

Turning off the first three, leaving the others alone more bits, but one of the same operations… 1 1 1 1 ? 1 1 1 1 1 AND can turn multiple bits off. (OR can turn multiple bits on.) CS447

Let's look at this again. 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 red green blue 1 0 1 1 1 1 0 1 1 1 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 1 1 CS447

…(animated) hmm 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 | 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 0 1 1 1 1 0 0 0 0 0 1 0 0 1 1 CS447

Left-shifting and ORing if you have the values of the fields and you want to put them together into a bitfield shift each value left to the correct bit position OR the shifted values together OR puts things together. for RGB565, red is shifted left 11 green is shifted left 5 blue isn't shifted (shifted left 0…) color = (red << 11) | (green << 5) | blue; CS447

Going the other way let's go from the bitfield to three separate values. 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 let's say we somehow set all the non-red bits to 0. 1 what value is this? it's not 23, that's for sure. so how do we fix that? CS447

It's the exact opposite we have to shift right to put the field at position 0 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 shift right by 11 and… 1 cool. what about green? shift right by…? 1 uh oh. CS447

Masquerade we need to get rid of (zero out) the bits that we don't care about a mask is a specially-constructed value that has: 1s in the bits that we want to keep 0s in the bits that we want to discard which bits do we want to keep? which do we want to discard? 1 & 1 this is the mask 1 CS447

Coming up with the mask value if you want to mask a 3 bit value, the mask is 1112 if you want to mask a 4 bit value, the mask is 11112 if you want to mask a 5 bit value, it's…? Size(n) Mask 2n Mask in hex 0x7 0xF 0x1F 3 4 5 1112 11112 111112 8 16 32 - in hex, the masks will look like 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF etc. CS447

Right-shifting and ANDing to extract one or more fields from a bitfield: shift the bitfield right to put the desired field at bit position 0 AND that with 2n-1, where n is the number of bits in the field or think of it as n 1 bits as a binary number AND takes things apart. so for RGB565… the red and blue masks are 1 11112 or 0x1F the green mask is 11 11112 or 0x3F red = (color >> 11) & 0x1F; green = (color >> 5) & 0x3F; blue = color & 0x1F; CS447

NOW it works 15 11 10 5 4 1 1 let's extract green 14 13 12 11 10 9 8 7 6 5 4 3 2 1 shift right by 5 and… 1 and then AND with 0x3F… 1 CS447

Can't you AND then shift? 15 11 10 5 4 1 1 sure, but… 14 13 12 11 10 9 8 7 6 5 4 3 2 1 AND with 0x7E0 (!)… 1 shift right by 5… 1 where did I get 0x7E0?? it's 0x3F << 5. I feel like that's uglier. CS447

lui, ori… lui at, 0xDEAD ori t0, at, 0xBEEF if I write li t0, 0xDEADBEEF in MIPS, the assembler turns it into: lui at, 0xDEAD ori t0, at, 0xBEEF the reason it splits it up is that there's only enough space in each instruction to fit half of 0xDEADBEEF what the heck are these instructions doing tho - we'll learn about instruction encoding later - but it suffices to say each immediate is 16 bits long CS447

By your powers combined… lui means load upper immediate. it puts the immediate value into the upper 16 bits of the register, and zeroes out the rest lui at, 0xDEAD then, ori does logical OR of at and its zero-extended immediate ori t0, at, 0xBEEF 1 1 | 1 1 1 1 1 1 D E A D B E E F CS447