• Current Events & Politics
    Welcome Guest
    Please read before posting:
    Forum Guidelines Bluelight Rules
  • Current Events & Politics Moderators: deficiT | tryptakid | Foreigner

Tech What is a zero-fill right/left shift in Java?

cowardescent

Bluelighter
Joined
Jun 29, 2017
Messages
404
I'm learning Java and going on the topic of bitwise operators. Specifically shifts and the OR operator. I'm curious if the or bitwise operator set the result bit to 1 if either two numbers are one. Must it be only one of the two numbers is one or can both be one? Isn't XOR the one where the numbers must be different for the result to be 1? Here is the table.

jxs5qGs.png
 
OR is one or both of them. Exclusive or (XOR) is one and only one, it will be 0 if they're both 1 or if they're both 0.
 
AND, OR, NOT, XOR are logic gates, the best way to understand these is to look up their truth tables.

Logic gates are mainly a hardware concept so it's not that common to see XOR used in programming. Mostly you'll see the short-circuit versions of AND and OR, && and ||. These operators try to resolve the statement just using the first operands and are harder to make mistakes with than & and |.

Bitshifting is mostly used as a substitute for standard arithmetic operators because it translates to fewer instructions in machine code. To understand bitshifting you need to learn how to convert binary (base 2) to decimal (base 10). It's not difficult just a nuisance. Left-most zeroes are meaningless so that saves a little time. In the example you have 1001 (9 in decimal) turned into 0010 (2 in decimal). because you lose the left-most 1, which means 8 in decimal, the number decreases because now the two left-most zeroes just mean 0 in decimal. You would also get 2 by converting 0000 0000 0000 0010.

Binary often is represented in groups of 4, which covers the range 0-15 in decimal. This makes it easier to translate a group into a hexadecimal value. Hex uses 0-9 then A-F to represent 0-15 in decimal. So 13 in hex is two different values and D is actually 13 in decimal. Sometime hex codes that mean things in English are used to represent errors, DEADBEEF being the most famous example. There's also CAFEBABE, which apparently some consider offensive now :\

the wiki article covers binary math in detail-

 
I fell down a wikipedia hole one night like a year or so ago, when I started looking into binary. I mean I have my Bachelor's degree in computer science so I have understood what binary is for some time, as well as hexadecimal. But we go with decimal, which is kind of odd, and apparently it almost didn't happen. Number systems are interesting, a base 12, for example, would have 0-9, a and b. "10" in hexadecimal is the same as 16 in decimal. I dunno, I've always liked math and numbers and understanding alternate number systems has helped me to understand ratios better. A base 10 system is pretty arbitrary, though obviously I'm quite used to it, since it's what we use.
 
I almost never touch binary by hand these days, but logic gates and boolean logic were a big part of electronics engineering. Binary is foundational to electronic circuits because you're dealing with the flow of direct current electricity so it's either on (1) or off (0) and everything is accomplished with those two states.

Really a CPU as just an aggregation of logic gates. Physical logic gates are fairly cheap so with a couple of those, some LEDs, a breadboard, few pieces of wire and a power supply you can build your own primitive CPU. Most PC PSUs have a 5V rail so they can be used as a bench power supply pretty readily.
 
There are only 10 kinds of people in the world:

Those who understand binary and those who don't ! :cool:

I wrote a lot of binary math operation code both for data compression and state machines for process control, but it was usually in assembler or C / C++ not java.
 
it’s not that arbitrary if you think about it :)

alasdair

It kinda is. It's usually considered that we use base 10 because we have 10 fingers. But other civilizations have used other systems. Like using the space between the fingers to reach base 8. Or using the knuckles to reach base 28.

But yes 10 is the most common. :)
 
I fell down a wikipedia hole one night like a year or so ago, when I started looking into binary. I mean I have my Bachelor's degree in computer science so I have understood what binary is for some time, as well as hexadecimal. But we go with decimal, which is kind of odd, and apparently it almost didn't happen. Number systems are interesting, a base 12, for example, would have 0-9, a and b. "10" in hexadecimal is the same as 16 in decimal. I dunno, I've always liked math and numbers and understanding alternate number systems has helped me to understand ratios better. A base 10 system is pretty arbitrary, though obviously I'm quite used to it, since it's what we use.

Yeah, any number system is arbitrary.

Hex, for example is not particularly useful unless you are notating in assembler. Each hex number resolves to 4 bits (a nibble), so two hex numbers are a very convenient shorthand method of notation for bytes. Even the bit/nibble/byte relationship is arbitrary because before "modern" conventions settled in, data structures were not always determined in this uniform method.

Base 10 is the Arabic Numbering System. Before this there was no "zero" to hold number places.

I can go on and on about numeric systems, but I think I'll stop there so as not to bore people.
 
I was trying to get a theoretical emphasis computer science degree. To fulfill those requirements I had to take more math than most CS majors. I discovered that , if I planned it well, I would end up with only 2 higher math courses above my requirements for the CS degree, to have a double major. Those 2 classes were Abstract Algebra and Abstract Calculus, hardest classes I took in college by far. I never use the higher math stuff, I use algebra and geometry and earlier all the time, but didn't need a degree for that. The kind of programming I do isn't very math-intensive.

I think the career options for a pure math degree are rather limited, or maybe you have a plan? I would make sure you have some plan for how you'll make a living doing what you study. I know too my people who went to school to go to school, got like a philosophy degree or something, and had no desire to go into academia as a profession and how their degree is worthless and they owe money on it. For me the math part was just because I could... the computer science was what I was going for, and I'm glad I did every day because my work situation is awesome and I make enough money to own my home, not live paycheck to paycheck, and be able to have nice things.
 
Neuroscience is my thing

Computational neuro to be more specific. It's a field that tends to bring math/CS/physics type folks. Theoretical neuroscience that can be applied to the artificial learning systems that are all the rage nowadays. Currently doing a BS neuroscience supplementing with plenty fo math, mainly out of my own curiosity/interest. By the time I finish up my degree I'll only be a few classes away from a double major myself. Then onto grad school lol
 
If you want to grok basic digital logic intuitively without having to bother with truth tables and Karnaugh maps; I'd recommend getting an Apple ][ emulator and tracking down a pair of old games called Rocky's Boots and Robot Odyssey. I didn't even realize at the time that I was learning binary and logic... I was just making a raccoon dance and robots do cool things. But those games... Robot Odyssey especially... sneak a damn lot of it into your gameplay in a way where you don't even realize you're learning. And don't let the fact that they're games fool you. Rocky's Boots is fairly basic. But Robot Odyssey will be fulfilling even for an adult. I still credit it for a good handful of A's I didn't have to try hard for in my own CS classes.

Yeah... it's for the Apple ][. So the graphics are rudimentary and you need that emulator. I'd recommend something more recent. But they *really* don't make educational games the way they used to.
 
Neuroscience is my thing

Computational neuro to be more specific. It's a field that tends to bring math/CS/physics type folks. Theoretical neuroscience that can be applied to the artificial learning systems that are all the rage nowadays. Currently doing a BS neuroscience supplementing with plenty fo math, mainly out of my own curiosity/interest. By the time I finish up my degree I'll only be a few classes away from a double major myself. Then onto grad school lol

Nice, soudsn like you have a plan. I was going to go to grad school originally but I moved to where my ex-wife got into grad school, and didn't get into the school I applied to around here. Then got a job instead and realized I am thankful I did not go to grad school. but then again, my skills allow me to excel at my job, in a really good worming situation (work from home/remote/anywhere), and not have to put too much of my energy into it so I have the time and energy to focus on being a musician which is really my passion. I think for what I'm doing, grad school would have been pointless and would have saddled me with a lot more debt. But if you have your sights set on a path that requires a graduate degree, then go for it, absolutely.
 
Nice, soudsn like you have a plan. I was going to go to grad school originally but I moved to where my ex-wife got into grad school, and didn't get into the school I applied to around here. Then got a job instead and realized I am thankful I did not go to grad school. but then again, my skills allow me to excel at my job, in a really good worming situation (work from home/remote/anywhere), and not have to put too much of my energy into it so I have the time and energy to focus on being a musician which is really my passion. I think for what I'm doing, grad school would have been pointless and would have saddled me with a lot more debt. But if you have your sights set on a path that requires a graduate degree, then go for it, absolutely.
Yeah the path I’m on does require one. I’ve known for a loooong time that I want to do research it just took me a while to figure out what i wanted to research. Taking drugs got me into neuro lol and then as I explored more of what the field actually is, I became fascinated with the math behind it. Eventually I’d like to pivot out of academia but I also would have no problems staying the course for a while. This is something I’m willing to work for
 
Top