Home General Chat
If you need urgent support, call 999 or go to your nearest A&E. To contact our Crisis Messenger (open 24/7) text THEMIX to 85258.
Read the community guidelines before posting ✨

Java Numerical Types

BillieTheBotBillieTheBot Posts: 8,718 Bot
I need to make a 4 bit number (for IP), but the smallest java supports is byte (8 bits).
I need to get 2 4-bit numbers into a byte.

Anyone know how to set individual bits?
i.e.

byte whatever = 10101010

also gotta use a 3 bit number as well, somewhere...

very grateful for any help
Beep boop. I'm a bot.

Comments

  • Former MemberFormer Member Posts: 1,876,323 The Mix Honorary Guru
    hmm I know PASCAL and i think MOD-2 have an integer type called BYTE with is just that.... Alternatively set up 8 boolean varialbes and manipulate each of them to give you an eight bit value...

    yes, the second suggestion was a joke, but it would work :)
  • Former MemberFormer Member Posts: 1,876,323 The Mix Honorary Guru
    Isn't it easier to store the 4 seperate parts as different numbers?
  • Former MemberFormer Member Posts: 1,876,323 The Mix Honorary Guru

    Isn't it easier to store the 4 seperate parts as different numbers?


    I assume when you say numbers you mean integers (?)

    (yes i am pedantic, sue me)

    SO, it probabily would be the easiest way of doing it, as it seems very unlikly you would ever need to carry out math functions on an IP address of greater complexity than adding 1.

    Remember to error check for numbers > 255 and <0
Sign In or Register to comment.