Help with bitwise

?

-

I've never used bitwise before. Can someone explain what does the
following mean:

"This parameter is a bitmask, with only 2 bits having any significance.
If bit 3 is set, the decimal value is 8."
 
R

Roedy Green

ve never used bitwise before. Can someone explain what does the
following mean:

"This parameter is a bitmask, with only 2 bits having any significance.
If bit 3 is set, the decimal value is 8."

see http://mindprod.com/jgloss/masking.html and
http://mindprod.com/jgloss/binary.html

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
H

hskrtroy

QUOTE: "If bit 3 is set, the decimal value is 8."

I'll use an example to try to explain this statement. Sorry, but I
don't know what "This parameter" is. It sounds like they are referring
to something specific to what you are reading.

Example:
A bit is either a zero or a one. A byte is made up of multiple bits.
Let's say you have an 8 bit byte. The "binary" value of a number is
only going to be represented by zero's and one's. The "decimal" value
is what everyone is used to dealing with (ie. 1, 2, 3, ..., 10, 11, 12,
...., 20, 21, 22, ..., etc). Here are some examples of binary number
and their decimal equivalents.
BINARY DECIMAL
-------------------- --------------
00000000 0
00000001 1
00000010 2
00000011 3
00000100 4
00000101 5
00000110 6
00000111 7
00001000 8

The way binary works is just like the way decimal numbers work except
that you can only go as high as 1 before you have to add one to the
next number to the left, whereas in decimal you can go to 9. What I
mean is that you could represent your decimal numbers with zeros to the
left as well, but once you reach 9, you need to add 1 to the number to
the left.

For example:
7 can be represented as 00000007
8 can be represented as 00000008
9 can be represented as 00000009
but when you reach the next number you have to add one to the number to
the left and start over at zero with the ones digit.
10 would then be 00000010

In the binary example above, 00001000, the one would be in the 3rd
place (or bit 3). Java's bitwise starts counting the binary digits at
zero. So, the first zero starting from the right is bit 0, the second
zero from the right is bit 1, the third zero from the right is bit 2,
and the 1 then is a bit 3.

Sorry, if I didn't expain this very well. Maybe Roedy's link will
explain it better.

Troy
 
H

Hal Rosser

- said:
I've never used bitwise before. Can someone explain what does the
following mean:

"This parameter is a bitmask, with only 2 bits having any significance.
If bit 3 is set, the decimal value is 8."

2 to the power of 3 is 8
2 to the power of <bitnumber> = <value of that bit>
HTH
 
Y

Yamin

well a good course on binary math would be good. Others have tried to
summarize, but I'll try and answer a bit about bitmask..

A bit-mask is used to isolate bits from a number. Consider the decimal
number 1536. If I want to know how many 'hundreds' or 10^2 there are,
i only need to look at the 3rd digit (from the right). In this case,
there are 5.

Similarily, in binary if I have the number 00010001 and I need to know
how many 2^4 there are, I look at the 5th digit. In this case, its a
1. So there is 16 in the number.

So, a bit mask is used to isolate particular bits. So far, we've just
been talking about isolating 1 digit at a time...To do this, you use a
binary AND operation. Its symbol is &.

suppose i have the binary number
01010101 and I want to AND it with a bitmask 00110001.
This would mean I am only interested in the 1st, 5th and 6th digts in
the number.

To do a binary AND, you go column by column. The result is a 1 if both
numbers are 1. the result is 0 otherwise...so in the case:
01010101
&00110001
=00010001

Generally speaking, bit masking is used to save space. If we take 1
byte (8 bits), we can pack 8 boolean values into it (each bit being one
value). We can also say the top three bits are a number and the rest
are booleans or whatever.

For example. Suppose I decide that the top 4 bits (value1) are a
number. The next bit is a boolean (value2), and the next 3 bits are a
number (value3).

My mask to isolate value1 is 11110000
My mask to isolate value2 is 00001000
My mask to isolate value3 is 00000111

Suppose I have a value of 10101100 and I want to check if value1 is
1010. How do I do that? First I isolate it, then I compare it knowing
all other values will be zero.

10101100 & 11110000 = 10100000
is 10100000 == 1010000
Yes!

Its also useful to use bit shifting and Oring, but anyways. You should
probably learn a bit more on binary arithmetic before programming at
that level.

Yamin Bismilla
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Help with code 0
Bitwise complement 4
Help with code plsss 0
Help 1
Help with array 4
Help with Loop 0
Help with Visual Lightbox: Scripts 2
Help with my responsive home page 2

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top