GithubHelp home page GithubHelp logo

epi's Introduction

EPI, 1 month prep

Hex to binary

0x1111 = 0b 1111 1111 1111 1111

Extract first 4 bytes of a word

void ExtractBytes (uint32_t value) {
  uint32_t byte1 = value >> 24;
  uint32_t byte2 = (value >> 16) & 0xff;
  uint32_t byte3 = (value >> 8) & 0xff;
  uint32_t byte4 = value & 0xff;
  ...
}

Count the # of 1 bits in an int

int CountBits (int x) {
  int num_bits = 0;
  while (x) {
    num_bits += x & 1;
    x >>= 1;
  }
  return num_bits;
}

Useful Bitwise Operations

Set xth bit (0 being rightmost bit)
number |= (1 << x);
0000 |= (1 << 3) => 0000 |= 1000 => 1000

Clear xth bit:
number &= ~(1 << x);
1111 &= ~(1 << 2) => 1111 &= ~(0100) => 1111 &= 1011 => 1011

Toggle xth bit, since 0^1=1 and 1^1=0:
number ^= (1 << x);
1111 ^= (1 << 1) => 1111 ^= 0010 => 1101
0000 ^= (1 << 1) => 0000 ^= 0010 => 0010

Get lowest bit in x that is set to 1
x & ~(x-1)
x = 1000; x-1 = 0111; ~(x-1) = 1000; x & ~(x-1) = 1000
x = 1011101; x-1 = 1011100; ~(x-1) = 0100011; x & ~(x-1) = 0000001
x = 10100; x-1 = 10011; ~(x-1) = 01100; x & ~(x-1) = 00100

Clear lowest bit in x that is set to 1
x & (x-1)
x = 1011101; x-1 = 1011100; x&(x-1) = 1011100;
x = 1010; x-1 = 1001; x&(x-1) = 1000;

  • 5.3
  • 5.11
  • 6.5
  • 6.8
  • 7.7
  • 7.8
  • 8.10
  • 9.3
  • 9.9
  • 10.11
  • 11.5
  • 12.5
  • 12.10
  • 13.4
  • 13.7
  • 14.6
  • 14.9
  • 15.5
  • 15.9
  • 16.4
  • 16.9
  • 17.5
  • 17.7
  • 18.8
  • 19.3
  • 20.9
  • 21.1

epi's People

Contributors

mzmousa avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.