Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. convert integer to 8 bits

convert integer to 8 bits

Scheduled Pinned Locked Moved Java
tutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lost_in_code
    wrote on last edited by
    #1

    hi all.. i want to convert an integer number into 8 bits .. for example : if the Integer number is 5 , i want it to be 00000101. and i need the zero's in my output.. i'll be very thankful if any one have an idea ..

    L 1 Reply Last reply
    0
    • L lost_in_code

      hi all.. i want to convert an integer number into 8 bits .. for example : if the Integer number is 5 , i want it to be 00000101. and i need the zero's in my output.. i'll be very thankful if any one have an idea ..

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      And what exactly are you having trouble with? Couldn't you just do something like:

      boolean[] b = new boolean[8];
      for (int i = 0; i < 8; i++)
      {
      b[7 - i] = (x & 1) == 1;
      x >>= 1;
      }

      warning: untested Or something that looks just like it. Perhaps an array of ints like this:

      int[] b = new int[8];
      for (int i = 0; i < 8; i++)
      {
      b[7 - i] = x & 1;
      x >>= 1;
      }

      Or perhaps not 7 - i but just i

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups