CSE 351
Section 2: Integer representations, two’s complement, and bitwise operators
CSE 351 Section 2: Integer representations, twos complement, and - - PowerPoint PPT Presentation
CSE 351 Section 2: Integer representations, twos complement, and bitwise operators Integer Representations Decimal (No prefix) 369845, -513 564U (unsigned) printf (%d, x); Binary (0b prefix)
Section 2: Integer representations, two’s complement, and bitwise operators
division and modulus to get each digit, tracking the remainder
remains
0000 0001 0011 1111 1110 1100 1011 1010 1000 0111 0110 0100 0010 0101 1001 1101 + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 – 0 – 1 – 2 – 3 – 4 – 5 – 6 – 7
programmer to understand?
using 4 bits?
0000 0001 0011 1111 1110 1100 1011 1010 1000 0111 0110 0100 0010 0101 1001 1101 + 1 + 2 + 3 + 4 + 5 + 6 + 7 – 8 – 7 – 6 – 5 – 4 – 3 – 2 – 1
int and returns 0 if the input is 0, else 1?
and 1 bit to represent, respectively
bits?
size if you don’t do so yourself
#include <stdio.h> typedef struct { int x : 3; int y : 4; int z : 1; int padding : 24; } Flags; int main(int argc, char* argv[]) { Flags flags = {3, 8, 1, 0x8fffff}; printf("sizeof(flags) is %ju and it stores 0x%x\n", sizeof(flags), *(int*) &flags); return 0; }
// Pack some values into a byte byte bitValue = 0; bitValue |= 3; bitValue |= 8 << 3; bitValue |= 1 << 7; // Unpack the values from the byte byte x = bitValue & 0x7; byte y = bitValue & 0x78; byte z = bitValue & 0x80; // Alternatively, we could have shifted a particular // mask instead, e.g. (0x1 << 7) instead of 0x80
suit value