• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Bitwise Arithmetic

 
Ranch Hand
Posts: 64
4
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have always wondered what happens "under the hood" when a machine performs integer/float arithmetic. In context of Java/C++, I'm wondering how to do integer/float addition, subtraction, multiplication, division, modulus, and maybe exponentiation and stuff using only the binary operators like &, ^, |, and the shift operators <<, >>, and >>>. I get that this may be a little unnecessary since we already have built-in operators like +, -, *, /, and %, but I would like to know deeper into how it works "under the hood".

I'm already familiar with the structure of Java's integer (byte to long) and float (float and double) types, but unsure how to operate on them.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Louis Denning wrote:I have always wondered what happens...


Sounds to me like two questions there:
1. How does binary (specifically, in the case of Java, 2's complement) arithmetic work.
2. How do I work with different integer/float types.

For the first, I suspect the link I provided might help; it should be pointed out that Java does NOT have a "rotate" operator, which is also used in some arithmetic functions (as I recall; it's been 30 years since I worried about that stuff ). All the wrapper classes have rotate() methods though.

For the second: I think you'll need to be more explicit. Specifically, what is it you don't understand: conversion? (widening or narrowing).

As for FP-stuff. I suggest you start here.

Winston
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How much under the hood do you want to go? Turtles go all the way down to the quantum level. In a nut shell, this is what happens at various layers when you do something like 1+ 2

1) JVM reads the opcode and issues a command to the OS
2) OS stores the operands in a memory location, and issues an ADD command to the CPU giving the memory location of the operands, and the memory location where the result should be
3) CPU sends a command to the RAM to fetch the first operand and stores it in a register. Similarly fetches the next operand and stores it in another register. Activates the correct internal circuits that performs the add operation that puts the result in a third register. Issue another PUT command that puts the contencts of third register into memor. I can go more into how the FETCH and PUT commands work but I won;t because they are not interesting
4) The way those internal circuits work is every arthimetic operation can be converted into a boolean formula. Circuit designers put together logic gates that perform the operation
5) The way logic gates are costructed by putting transistors together in certain configuration that perform the AND, OR and NOR function
6) The way transistors work is that they are semiconductors whose conductiveness can be controlled electrically
7) The way semiconductors work is that they are made up of silicon that has an atomic weight of 14. SInce SI has 14 electrons, which means it has 4 valence elctrons, that make it suitable to be able to control it's conductiveness by applying an external field
8) The reason SI has 14 valence electrons is because quantum theory!!! You lost me there actually.. That's the extent of my knowledge

So, yeah, each of these are semester long courses in a Computer Engineering course. Learning everything takes you 4 years. Forgeting everything takes you 4 beers. You want to learn it on your won, you will have to find the right books.
 
Marshal
Posts: 69843
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult for “beginning”. Moving thread.
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jayesh A Lalwani wrote:Learning everything takes you 4 years. Forgeting everything takes you 4 beers...


Temporarily. I'm a really old CAMRA member, and I still get flashbacks to ICL PLAN code. Especially LDX 0.

Winston
 
I can't beleive you just said that. Now I need to calm down with this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic